Compress file using command
The command updates Draft. Zip archive. Specifies how much compression to apply when you're creating the archive file. Faster compression requires less time to create the file, but can result in larger file sizes.
This parameter is required and specifies the path to the archive output file. The DestinationPath should include the name of the zipped file, and either the absolute or relative path to the zipped file. If the file name in DestinationPath doesn't have a. Specifies the path or paths to the files that you want to add to the archive zipped file. Unlike the Path parameter, the value of LiteralPath is used exactly as it's typed. No characters are interpreted as wildcards.
If the path includes escape characters, enclose each escape character in single quotation marks, to instruct PowerShell not to interpret any characters as escape sequences. To specify multiple paths, and include files in multiple locations in your output zipped file, use commas to separate the paths.
To specify multiple paths, and include files in multiple locations, use commas to separate the paths. This parameter accepts wildcard characters.
Wildcard characters allow you to add all files in a directory to your archive file. Updates the specified archive by replacing older file versions in the archive with newer file versions that have the same names.
You can also add this parameter to add files to an existing archive. The cmdlet only returns a FileInfo object when you use the PassThru parameter. Using recursion and sending objects down the pipeline can duplicate files in your archive. For example, if you use Get-ChildItem with the Recurse parameter, each FileInfo and DirectoryInfo object that's sent down the pipeline is added to the archive. Other ZIP archive tools may use a different encoding scheme.
When extracting files with filenames not stored using UTF-8 encoding, Expand-Archive uses the raw value found in the archive. This can result in a filename that is different than the source filename stored in the archive. Skip to main content. Join , subscribers and get a daily digest of news, geek trivia, and our feature articles. By submitting your email, you agree to the Terms of Use and Privacy Policy. The ZIP file format reduces the size of files by compressing them into a single file.
This process saves disk space, encrypts data, and makes it easy to share files with others. It takes the path to any files you want to compress—multiple files are separated with a comma—and archives them in the destination you specify. Note: Quotations around the path are only necessary when the file path contains a space.
In the previous example, we put the path to a directory with multiple files and folders in it without specifying individual files. PowerShell takes everything inside of the root directory and compresses it, subfolders and all.
When you use the character, you can exclude the root directory, compress only files in a directory, or choose all files of a specific type. To use a wildcard with Compress-Archive, you must use the -Path parameter instead, as -LiteralPath does not accept them. Above, we covered how to include the root directory and all of its files and subdirectories when creating an archive file.
However, if you want to exclude the root folder from the Zip file, you can use a wildcard to omit it from the archive. It should look something like this:. Next, say you have a folder with a bunch of different file types. You can tell PowerShell to archive them without touching the others explicitly.
It would look something like this:. Even after the archive is complete, you can update an existing zipped file with the use of the -Update parameter. It lets you replace older file versions in the archive with newer ones that have the same names, and add files that have been created in the root directory. It will look something like this:.
This is where our friend tar comes in. We could delve into piping the output from tar into gzip to create a compressed tarball, but tar makes things easy by allowing us to pass in an argument to tell it to use gzip for compression.
Compressing a couple of files is all well and good, but often times we need to compress a bunch of files that are all in a single directory. To compress an entire directory with zip simply include the -r or --recurse-paths argument:.
Similar to compressing multiple files with gzip , we will need to leverage tar to take the directory and create an archive that we can then compress:. Also worth noting that by default, tar will include the full, absolute path for the files it adds to an archive.
We use this flag whenever we need to create a new archive. When we specify this flag, it means that archive will be created using gzip compression. Providing the -v flag shows details of the files compressed.
Archive file names are mapped using the -f flag. We use this flag when files need to be extracted from an archive. How to create an archive We have a list of the following files which we'll compress with tar. List of files to be compressed. In the results below, we can see that the archive has been created successfully. Archive has been created with supplied command. How to remove files after compression Let's say we don't want to keep the original files after creating an archive.
How to view the contents of an archive You might need to view the contents of an archive without actually extracting it. Viewing contents of an archive.
0コメント