4

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.

barlop
  • 23,380
  • 43
  • 145
  • 225
user110242
  • 41
  • 1
  • 1
  • 2
  • 1
    Possible duplicate of [How to zip files in XP's command line without additional tools/downloads](http://superuser.com/questions/340670/how-to-zip-files-in-xps-command-line-without-additional-tools-downloads) – Canadian Luke Nov 09 '15 at 15:27
  • Used to use pkzip for (real) DOS back in the day, 25 years ago. Since then i've rarely done compressing from command line but when I did on Windows, I used 7-zip, 7zip does have a command line. When you say without external software, you mean without 3rd party software. Using only native methods. – barlop Nov 24 '19 at 20:43
  • Does this answer your question? [Create .zip folder from the command line - (Windows)](https://superuser.com/questions/201371/create-zip-folder-from-the-command-line-windows) – cowlinator Apr 21 '20 at 21:52

3 Answers3

5

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

cowlinator
  • 734
  • 1
  • 8
  • 16
  • 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](https://superuser.com/help/privileges/close-questions) or, if you don't have enough reputation for that, [raise a flag](https://superuser.com/help/privileges/flag-posts) 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 '20 at 21:49
  • One limitation is PowerShell can only compress files of up to 2GB in size (as pointed in the linked documentation). – Juha Palomäki Dec 25 '22 at 07: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

BruceWayne
  • 101
  • 2
  • the file is colored in blue, the compression ratio is 1.0. in this mathod I can create a zip/rar file? – user110242 Nov 09 '15 at 08:26
  • 3
    Please read the question again carefully. Your answer does **not** answer the original question. `compact` does not create zip files. – DavidPostill Nov 09 '15 at 11:04
  • 1
    @DavidPostill extremely poorly asked question.. very unnecessarily insufficient info in the title. I just adjusted the title – barlop Nov 24 '19 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. – Juha Palomäki Dec 25 '22 at 08:00
-1

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.