简体   繁体   中英

Powershell Creates Different Zip File?

I'm creating a zip file from a directory in two ways in Win10.

The first method using "Right-Click...Send To...Compressed (zipped) folder"

The second method using Powershell Compress-Archive. I wanted to automate the process using Powershell.

The zip file is a library to be imported into the Arduino IDE.

The first method will import into the Arduino IDE correctly. The second method does not import correctly.

Now before you say this is a Arduino problem please continue to read.

So to make sure the files are the same I used Powershell to generated MD5 checksums for both files. To my surprise they were different!!

The only difference I have found are the size of the created zip files. They are created from the same source files.

Why are they different? I can manually unzip both files by "Double-Clicking and Extract All" within File Explorer

Thank you for any advice/help. John.

Here is output of the powershell script...

Directory: C:\file1 Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 05/16/2020 17:18 1948 t_OS.zip

Algorithm: MD5 Hash: 19249C2BB9B50E827A77F7A2952EFF6D Path: C:\file1\t_OS.zip

Directory: C:\file2 Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 05/16/2020 19:23 1724 t_OS.zip

Algorithm: MD5 Hash: 2BF7B111F54FC0555E6CA64719AD350F Path: C:\file2\t_OS.zip

...

#
# sourceFiles are what I need to be zipped. It contains the 
# directory t_OS and I need to create t_OS.zip
#
$sourceDir   = "C:\sourceFiles" 
$sourceFiles = "C:\sourceFiles\t_OS"
# file1 zip was created by right-clicking on the directory t_OS
# in the $sorceDir and selecting Send To, then 
# Compressed (zipped) folder.  This file was then moved to...
# 
$file1 = "C:\file1\t_OS.zip"

# file2 zip is created by powershell using Compress-Archive below.
$file2 = "C:\file2\t_OS.zip"

Get-ChildItem -LiteralPath $sourceDir -Name -Recurse

If (Test-Path $file2)   {Remove-Item $file2}

Compress-Archive -DestinationPath $file2 -CompressionLevel Fastest -Path $sourceFiles

Get-ItemProperty -LiteralPath $file1
Get-FileHash -Path $file1 -Algorithm MD5

Get-ItemProperty -LiteralPath $file2
Get-FileHash -Path $file2 -Algorithm MD5

...

The fact that the two zip files are different sizes can have a number of reasons. Here are a few

  1. As already mentioned, the two zip files could be using different compression algorithms. Given the small size of the resultant zip files, it may be that one is storing the data compressed, while the other is storing it uncompressed.
  2. There are a number of extensions that have been added to zip files over the years that have added things like more accurate timestamps. Those add more bytes the the resultant zip files, but they shouldn't prevent the file from being uncompressed. I believe a "right-click" created zip file on Windows 10 doesn't use any of these extensions. Not sure about Powershell.

Do you have access to a Linux setup? There are tools on it that can analyse the structure of zip files.

Also, can you share the Powershell command line you used to create the zip file?

Edit

On your Ubuntu setup, start by checking what unzip thinks about the two files

unzip -t t_OS.zip

If unzip finds an issue it will report it.

If that doesn't work run zipdetails against both zip files and update your question with the results.

zipdetais -v t_OS.zip

If you don't have zipdetails on your Ubuntu setup, get it from here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM