Last Update: Sep 04, 2024 | Published: Mar 08, 2012
Maybe it is my Midwestern heritage, but I like to be thrifty and economical. Perhaps that is why I like file compression; I can keep the file but only consume a minimal amount of space. Back in the day we typically turned compression on at the folder level, which is still probably a good thing. But sometimes you might want to take a more granular approach. Turns out you can accomplish both with a command line tool called Compact.exe.
First, how do we use Compact.exe? As with most command line tools, let’s start by asking for help:
C:> compact /?
Figure 1: Compact.exe Help
Now let’s check the status of folder. For this to work properly you need to be at the folder root.
C:work>compact /q Listing C:work New files added to this directory will not be compressed. Of 445 files within 1 directories 0 are compressed and 445 are not compressed. 962,585,132 total bytes of data are stored in 962,585,132 bytes. The compression ratio is 1.0 to 1.
This folder has no compressed files. If you run the command without /Q you’ll get details for all the files. I know there are a lot of text files which compress well so I’m going to compress them like this:
C:work>compact /c /s *.txt Compressing files in C:work a.txt 322158 : 81920 = 3.9 to 1 [OK] a2.txt 322174 : 81920 = 3.9 to 1 [OK] add-perm.txt 5444 : 4096 = 1.3 to 1 [OK] addpermission.txt 1829 : 1829 = 1.0 to 1 [OK] addpermission2.txt 8643 : 4096 = 2.1 to 1 [OK] … Compressing files in C:worksecret New Text Document.txt 37 : 37 = 1.0 to 1 [OK] 115 files within 25 directories were compressed. 13,014,911 total bytes of data are stored in 3,799,246 bytes. The compression ratio is 3.4 to 1.
That looked pretty successful! The /S told the utilty to recurse through all the subfolders as well and compress any TXT files. I have a few other file types in C:Work that will also compress well.
C:work>compact /c /s *.csv *.xml *.ps1
After looking at the results almost all of the folder is compressed so I might as well simply turn on compression at the folder level. This ensures that any new file will automatically get compressed.
C:work>compact /c /s . Setting the directory C:work to compress new files [OK] 1 files within 1 directories were compressed. 0 total bytes of data are stored in 0 bytes. The compression ratio is 1.0 to 1.
This didn’t compress any additional files, just marked the folder as compressed. If at some point I felt the need to uncompress everything it is this easy:
C:work>compact /u /s /i *.* C:work>compact /u /s . Setting the directory C:work not to compress new files [OK] 1 files within 1 directories were uncompressed.
Now all the files are uncompressed and C:Work is back to normal.
Compact.exe is a very handy and easy to use utility, but I suggest testing it out in a non-production environment to avoid any surprises.