简体   繁体   English

Powershell 创建不同的 Zip 文件?

[英]Powershell Creates Different Zip File?

I'm creating a zip file from a directory in two ways in Win10.我在 Win10 中以两种方式从一个目录创建一个 zip 文件。

The first method using "Right-Click...Send To...Compressed (zipped) folder"使用“右键单击...发送到...压缩(zipped)文件夹”的第一种方法

The second method using Powershell Compress-Archive.第二种方法使用 Powershell Compress-Archive。 I wanted to automate the process using Powershell.我想使用 Powershell 自动执行该过程。

The zip file is a library to be imported into the Arduino IDE. zip文件是要导入Arduino IDE的库。

The first method will import into the Arduino IDE correctly.第一种方法会正确导入Arduino IDE。 The second method does not import correctly.第二种方法导入不正确。

Now before you say this is a Arduino problem please continue to read.在你说这是一个 Arduino 问题之前,请继续阅读。

So to make sure the files are the same I used Powershell to generated MD5 checksums for both files.因此,为了确保文件相同,我使用 Powershell 为两个文件生成 MD5 校验和。 To my surprise they were different!!令我惊讶的是他们是不同的!

The only difference I have found are the size of the created zip files.我发现的唯一区别是创建的 zip 个文件的大小。 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...这是 powershell 脚本的 output...

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

Algorithm: MD5 Hash: 19249C2BB9B50E827A77F7A2952EFF6D Path: C:\file1\t_OS.zip算法:MD5 Hash: 19249C2BB9B50E827A77F7A2952EFF6D路径:C:\file1\t_OS.zip

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

Algorithm: MD5 Hash: 2BF7B111F54FC0555E6CA64719AD350F Path: C:\file2\t_OS.zip算法:MD5 Hash: 2BF7B111F54FC0555E6CA64719AD350F路径: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.两个 zip 文件大小不同的事实可能有多种原因。 Here are a few这里有一些

  1. As already mentioned, the two zip files could be using different compression algorithms.如前所述,两个 zip 文件可能使用不同的压缩算法。 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.鉴于生成的 zip 个文件很小,可能一个存储压缩数据,而另一个存储未压缩的数据。
  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.多年来,已将许多扩展添加到 zip 文件中,这些扩展添加了诸如更准确的时间戳之类的内容。 Those add more bytes the the resultant zip files, but they shouldn't prevent the file from being uncompressed.那些添加了更多字节的结果 zip 文件,但它们不应该阻止文件被解压缩。 I believe a "right-click" created zip file on Windows 10 doesn't use any of these extensions.我相信“右键单击”在 Windows 10 上创建的 zip 文件不使用任何这些扩展名。 Not sure about Powershell.不确定 Powershell。

Do you have access to a Linux setup?您可以访问 Linux 设置吗? There are tools on it that can analyse the structure of zip files.上面有工具可以分析zip个文件的结构。

Also, can you share the Powershell command line you used to create the zip file?另外,您可以共享用于创建 zip 文件的 Powershell 命令行吗?

Edit编辑

On your Ubuntu setup, start by checking what unzip thinks about the two files在您的 Ubuntu 设置中,首先检查unzip对这两个文件的看法

unzip -t t_OS.zip

If unzip finds an issue it will report it.如果unzip发现问题,它将报告它。

If that doesn't work run zipdetails against both zip files and update your question with the results.如果这不起作用,请针对两个 zip 文件运行zipdetails并使用结果更新您的问题。

zipdetais -v t_OS.zip

If you don't have zipdetails on your Ubuntu setup, get it from here如果您的zipdetails设置中没有 zip 详细信息,请从 此处获取

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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