As a Windows Network Administrator I always have to make space delete unneeded files, archive old files, compress files that we don’t need but we might need at some point.
I don’t use the same compression method every time, because the scenario is different. Here are a three compression methods for Windows.
Compress Files Using the NTFS Compression
This method of compression only works on NTFS partitions. You don’t have the option for a FAT or FAT32 partition.
The most usual and easier way to do this is to use the Windows’ Graphical Interface and access the Advanced File Properties and select Compress contents to save disk space.
Compact – Compress files or folders from command line
If you would like to compress files from a batch file use this command line variant. Usually if you access the advanced file properties, (right-click => properties and => advanced) you have the option to compress contents to save space. Another good use of the command line is that it gives you a better view of the progress. The graphic interface sometimes hangs and you don’t know if the job is still active.
The command to compress a folder from within the folder is compact /c /s. It compresses all of the files within the folder and marks the folder as compressed so that new files are created compressed.
Why would you choose Compact and not Zipped Folder?
The main reason is that a compacted folder can be used by applications. In other words if the path of a log file points to a compacted folder this is transparent for the application that writes the log file. As a result your log files will be compacted. You cannot do this with Zipped folders or files.
Compress files using the Windows’ built-in zip archiving utility
Why would you choose Compressed (Zipped) Folder over the Compacted Folder?
The zipped Folder has better compression and better portability. The zipped folder has a slightly better compression rate, and you can copy the files to any other Operating System, send them by email and they will retain the compression. The compacted folders will only be compressed on the original location unless you compress the file again at the new destination. Note in the image below the difference between the two file compression formats. The Windows shell has the option to “show NTFS compressed files in color”, which is a great option. Note that the zipped file is slightly smaller than the compacted, (blue colored), one.
Is there a Compressed (zipped) Folder – command line variant?
Unfortunately, there isn’t any Windows built-in option. The good news though is that there is a free archiver that has a command line version as well. 7zip is a great free utility very flexible that manipulates all of the popular archiving file formats.
Compressing from the command line with 7zip is very simple, the simplest command is 7z a NewFolder.zip “New Folder” that compresses the folder New Folder into the archive: NewFolder.zip. For more options and switches type: 7z –h, note that 7z doesn’t understand the /? switch, usual on any Windows application.
Why would you choose 7Zip over the Windows’ built in compressing solutions?
The main reason is flexibility. 7zip has a lot of options/switches that control a lot of the compression aspects and it is very easy to use in a batch file.
An interesting application is to separately archive a lot of folders from a batch file. Let’s say you have 500 folders that you want to archive and you know you will be accessing those folders on a regular. It makes more sense to archive the folders separately and not in one big file. It is easier to access smaller zip files from the Windows shell and keeps the system responsive. To do this manually for 500 folders is a nightmare. Here is a command that runs from a batch file that will compress those folders separately, each folder one zip file:
for /D %%d in (*.*) do 7z a -tzip “%%d.zip” “.%%d*”
To run the command from the command line and not from a batch file the command is slightly different:
for /D %d in (*.*) do 7z a -tzip “%d.zip” “.%d*”
There are other great archiving utilities such as Winzip, Winrar, Pkzip, etc… They are not free, but you can get a trial-ware which in many cases is good enough for anybody.
A few tips on what to compress and what not.
Usually Installation files are already compressed so there is no benefit in compressing.
Digital photos in a jpg or gif format are already compressed using special algorithms.
PDF files don’t compress.
Text files, (plain text, log files, etc…) compress the best.
Word files can be compressed but not as much as the text files.