5

I am looking for a command line command to comress a file. I want to compress each file individually to filename.zip.

all the methods that I found including: http://exchangeserverpro.com/powershell-script-iis-logs-cleanup/

and:

https://stackoverflow.com/questions/17546016/how-can-you-zip-or-unzip-from-the-command-prompt-using-only-windows-built-in-ca/26843122#26843122

are near by but not exactly what I need. any suggestion? if it possible, I would like it to happen wthout external software (like 7-zip). it can be a power shell script.

3

3 Answers 3

8

There is a single, simple cmd.exe command for this. (Through PowerShell v5.0+)

To zip:

powershell Compress-Archive -LiteralPath 'C:\mypath\testfile.txt' -DestinationPath "C:\mypath\Test.zip"

To unzip:

powershell Expand-Archive -LiteralPath "C:\mypath\Test.Zip" -DestinationPath "C:\mypath" -Force

Sources:

Special thanks to @Ramhound

2
  • Please do not post the same answer to multiple questions. If the same information really answers both questions, then one question (usually the newer one) should be closed as a duplicate of the other. You can indicate this by voting to close it as a duplicate or, if you don't have enough reputation for that, raise a flag to indicate that it's a duplicate. Otherwise tailor your answer to this question and don't just paste the same answer in multiple places.
    – DavidPostill
    Apr 21, 2020 at 21:49
  • One limitation is PowerShell can only compress files of up to 2GB in size (as pointed in the linked documentation). Dec 25, 2022 at 7:59
0

You can compress files without any any third party software

compact [{/c|/u}] [/s[:dir]] [/a] [/i] [/f] [/q] [FileName[...]]

for more check below link https://technet.microsoft.com/en-us/library/bb490884.aspx

4
  • the file is colored in blue, the compression ratio is 1.0. in this mathod I can create a zip/rar file?
    – user110242
    Nov 9, 2015 at 8:26
  • 3
    Please read the question again carefully. Your answer does not answer the original question. compact does not create zip files.
    – DavidPostill
    Nov 9, 2015 at 11:04
  • 1
    @DavidPostill extremely poorly asked question.. very unnecessarily insufficient info in the title. I just adjusted the title
    – barlop
    Nov 24, 2019 at 20:47
  • Compress seems to be quite inefficient compared to zip. Tried with 2GB source file (xml logs). Compress got size to 500MB, Windows built-in zip to 24MB. Dec 25, 2022 at 8:00
-2

there is no good/ easy way of doing, what yo're asking for. It is possible to do it using PowerShell /.Net as described in the topics you link to in the question, but this is quite complicated. Much easier solutions are to use third party tools like GZIP or 7-zip, or to use Windows build-in compress command, rather than using zip-files.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .