7zip hacks for Sitecore developers

In this post, I share how you can use 7-Zip command-line tool to automate some tasks when dealing with .zip files. Additionally, I share two simple commands that can help you to remove some undesirable folders from a zipped website and remove specific languages from a Sitecore package.

7-Zip

7-Zip is free open source software that zip files to several different formats. Some of theses formats are .zip, .7z, .gzip, among others. The official download page is 7-zip. It also comes with a command-line tool that you can use to automate some manual tasks.

To get started, I have added the 7-Zip installation folder to the environment variable Path in my machine, so I don't have to reference the full path "C:\Program Files\7-Zip" every time I want to run the command. I just type 7z and the magic happens.

The official 7-Zip documentation is located in the "C:\Program Files\7-Zip\7-zip.chm" file, but you can find some resources online as well. Some examples are 7-Zip Command Line Examples — All Syntaxes Shared Here and documentation.help.

Command-line and switches

For the examples I share in this post, I use the following command and switches:

  • d (Delete) command. Deletes files from archive. In the example below, the command deletes *.bak files from archive archive.zip.
7z d archive.zip *.bak -r
  • -r (Recurse subdirectories) switch. Specifies the method of treating wildcards and filenames on the command line. In the example below, the command lists all *.doc files that belong to the archived root directory in the archive.zip archive.
7z l archive.zip *.doc -r-

7-Zip uses wild name matching similar to Windows 95:

  • '*' means a sequence of arbitrary characters.
  • '?' means any character.

7-Zip doesn't use the system wildcard parser. 7-Zip doesn't follow the archaic rule by which *.* means any file. 7-Zip treats *.* as matching the name of any file that has an extension. To process all files, you must use a * wildcard.

Examples:

  • *.txt means all files with an extension of ".txt"
  • ?a* means all files with a second character of "a"
  • *1* means all names that contains character "1"
  • *.*.* means all names that contain two at least "." characters

The default wildcard "*" will be used if there is no filename/wildcard in the command line.

Slash ('\') at the end of a path means a directory. Without a Slash ('\') at the end of the path, the path can refer either to a file or a directory.

That's all we need to start automating some tasks when working with Sitecore.

Remove languages from package

Let's suppose you created a package with some content items, but this package has some languages that you don't want to be installed. You meant this package to have only the English language.

Normally, you would have to create a new package filtering just the languages you want, but alas you can alter the .zip file by running the following 7z command.

7z d package.zip -r en-BE en-DE en-FR en-NG en-TR fr-CA es-AR es-CL pt-ZA th-TH vi-VN zh-HK zh-TW ar-EG cs-CZ de-AT de-CH
Notice that I run the command against a package.zip. This is the zip that is inside the Sitecore package. This means you have to unzip the Sitecore package first, run the 7-Zip command and zip the change package.zip file again.

Remove large unneeded website folders

When you are not working with Docker, it's quite common to ask a colleague to zip his website folder and sent it to you so you can speed up the process of installing Sitecore. Or sometimes, you want the website folder from a production environment so you can run a compare tool to check for differences between installations.

In this process, a lot of files that you don't need are added into the zip. Some of them are quite large.

The command below removes from the zip file some of the folders that have large amount of files.

7z d website.zip -r temp App_Data\logs App_Data\MediaCache App_Data\mediaIndexing App_Data\packages App_Data\viewstate App_Data\diagnostics

If you want to preserve the folders and just delete the files, then run the following command:

7z d website.zip -r temp App_Data\logs\* App_Data\MediaCache\* App_Data\mediaIndexing\* App_Data\packages\* App_Data\viewstate\* App_Data\diagnostics\*

Credits

Photo by Anna Shvets from Pexels.

comments powered by Disqus