DEV Community

Aizaz Ahmad
Aizaz Ahmad

Posted on

Compress Folders like a PRO (Window)

Bored of using the GUI to Compress the folders? Then you have come to the right Blog that will change the way you Compress your folders/files forever. Today, I will be covering how to use Windows PowerShell to Compress the Folder/File and also exclude or Include specific folders/files while Compressing folders.


Simple Compression Operation

Open up Windows PowerShell and type in the Compress-Archive Command following the below Syntax:

Compress-Archive path/to/folder/file Compresed_File_name
Enter fullscreen mode Exit fullscreen mode

Path to the Folder/File can be both Absolute or Relative. You can also navigate to the Parent Folder of the File or Folder you want to Compress using the cd command and run the Command here too.

Examples

  1. To Create a Compressed ZIP file with name 'assets.zip' of 'images' folder inside the cwd (Assuming that we have already navigated to the parent Folder of 'images')

    Compress-Archive images assets
    
  2. Similarly for a file named 'img.png', while creating 'images.zip' inside the 'dir' folder the command will look like this:

    Compress-Archive img.png dir/image
    

Now, You will be like, Aizaz, WTH, What's Interesting in this? I can like get the above work done in just 2 clicks and you are asking me to write these Scary Commands to get the work done. Yes, you are right but these are just the basic stuff. I will be now explaining more interesting and amazing parts of it.


Creating a Single ZIP file from Multiple Folders/Files

Using the above same command, We can also create a single ZIP file from multiple resources (Folders/Files) by using the command with the following syntax:

Compress-Archive @('./path/to/1st/resource', 'path/to/2nd/resource', ...) Compresed_File_name
Enter fullscreen mode Exit fullscreen mode

For Example:

Compress-Archive @('file.txt', 'folder-1', 'folder-2') dir/example
Enter fullscreen mode Exit fullscreen mode

This will create a'example.zip' file inside the 'dir' folder with file.txt, folder-1, and folder-2 compressed.

This above Syntax can be used to choose specific files/folders inside a folder to be included in the archive. Just navigate to the folder containing the content you want to Compress and specify the files you want to include in the ZIP file.


Creating ZIP file Excluding Specific files/folders

There comes many situations where one has to exclude particular files/folders while creating a ZIP file. One kind of situation is when one has to send the Archive (Zip File) of a JavaScript Application that has node_modules installed, but one can't afford node_modules to be included inside a ZIP file as it will be troublesome to send such a huge file over the internet. One way of Getting Rid of this is to create a new folder and place all files other than node_modules in it and then compress that folder and send it. It Works, but this practice becomes weary when you have to do that again and again after changes in Source Code. But, now that's not the problem anymore.
Use the Following Command to Exclude file/folder:

Get-ChildItem -exclude file/folder_to_be_excluded | Compress-Archive -DestinationPath Compresed_File_name
Enter fullscreen mode Exit fullscreen mode

Run this Command after navigating to the directory you want to Compress. Get-ChildItem displays the list of contents of the Directory.
-exclude flag followed by the resource name is used to exclude that resource from the list. This list is passed to the Compress-Archive Command through the pipeline | that creates the ZIP file at the path specified after the -DestinationPath command.

Examples

Get-ChildItem -exclude node_modules | Compress-Archive -DestinationPath app
Enter fullscreen mode Exit fullscreen mode

You can also exclude multiple resources

Get-ChildItem -exclude @('node_modules', 'assets/img') | Compress-Archive -DestinationPath app
Enter fullscreen mode Exit fullscreen mode

We can also use an alias for Get-ChildItem command to make it simpler:

gci -exclude @('node_modules', 'assets/img') | Compress-Archive -DestinationPath app
Enter fullscreen mode Exit fullscreen mode

You can also Check out the documentation of both Get-ChildItem and Compress-Archive Commands to get detailed insight of these.

Thank you for your time and attention!

Discussion (3)

Collapse
usmanabdullahtech profile image
Usman Abdllah

thank you for writing and awesome blog. keep it up

Collapse
hafizab63861462 profile image
Hafiz Abdullah

Really help ful 😍❤️❤️

Collapse
haweras profile image
Haweras

Intresting!