简体   繁体   English

将使用java.util.zip创建的zip大小与使用java的原始文件夹大小进行比较

[英]Compare the size of zip created using using java.util.zip to original folder size using java

I have just created a Zip file using java.util.zip. 我刚刚使用java.util.zip创建了一个Zip文件。 Now I would want to check if the ZIP created is correct and the uncompressed size of the ZIP file is equal to the actual folders that I have zipped. 现在,我想检查创建的ZIP是否正确,并且ZIP文件的未压缩大小等于压缩后的实际文件夹。

I know of a method ZipFile.isValid() which returns true if all the headers in the ZIP are correct. 我知道一个方法ZipFile.isValid(),如果ZIP中的所有标头均正确,则返回true。 Doesn't solve my problem though. 虽然不能解决我的问题。

Thanks :) 谢谢 :)

Unzip the *.zip file, iterate over each ZipEntry element, sum each ZipEntry.getSize(). 解压缩* .zip文件,遍历每个ZipEntry元素,对每个ZipEntry.getSize()求和。 Compare this to the sum of the file sizes you zipped. 将此与您压缩的文件大小之和进行比较。 Alternatively (if you don't trust the ZipEntry headers for some reason) unzip each ZipEntry, counting the bytes, but discarding them. 或者(如果由于某种原因不信任ZipEntry标头)则将每个ZipEntry解压缩,计算字节数,然后丢弃它们。 You might do either of these things say, as a quick check of your zip code, or maybe even in a unit test. 您可能会做这些事情之一,例如快速检查邮政编码,甚至在单元测试中。

You can iterate over the ZipEntry's thusly: 您可以这样遍历ZipEntry:

zipInStream = new ZipInputStream(new FileInputStream(zipFile));
ZipEntry zipEntry;
while ((zipEntry = zipInStream.getNextJarEntry()) != null) {
    String entryName = zipEntry.getName();
    ....
}

Checking if the ZIP file is created correctly 检查ZIP文件是否正确创建

if (myZipFile.isValid())
{
    // The file has been created successfully
}

Knowing the Length of the directory and the file. 知道目录和文件的长度。

File dir = new File("path/to/the/directory/");
int size = dir.length();

File zip = new File("path/to/zipfile.zip");
int zipSize = zip.length();

Now you can compare them. 现在您可以比较它们。

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

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