Java 关于用于压缩文件的免费库的建议

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/166340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:59:09  来源:igfitidea点击:

Recommendations on a free library to be used for zipping files

javaencryptionzip

提问by nearly_lunchtime

I need to zip and password-protect a file. Is there a good (free) library for this?

我需要对文件进行压缩和密码保护。是否有一个好的(免费)图书馆?

This needs to be opened by a third party, so the password protection needs to work with standard tools.

这需要由第三方打开,因此密码保护需要使用标准工具。

采纳答案by JodaStephen

UPDATE 2020: There are other choices now, notably Zip4J.

2020 年更新:现在还有其他选择,尤其是Zip4J



After much searching, I've found three approaches:

经过大量搜索,我找到了三种方法:

A freely available set of source code, suitable for a single file zip. However, there is no license. Usage is AesZipOutputStream.zipAndEcrypt(...). http://merkert.de/de/info/zipaes/src.zip(https://forums.oracle.com/forums/thread.jspa?threadID=1526137)

一套免费提供的源代码,适用于单个文件 zip。但是,没有许可证。用法是 AesZipOutputStream.zipAndEcrypt(...)。 http://merkert.de/de/info/zipaes/src.zip( https://forums.oracle.com/forums/thread.jspa?threadID=1526137)

UPDATE: This code is now Apache licensed and released at https://github.com/mobsandgeeks/winzipaes(exported from original home at Google code) . It worked for me (one file in the zip), and fills a hole in Java's opens source libraries nicely.

更新:此代码现已获得 Apache 许可并在https://github.com/mobsandgeeks/winzipaes 上发布(从Google 代码的原始主页导出)。它对我有用(zip 中的一个文件),并且很好地填补了 Java 开源库中的一个漏洞。

A commercial product ($500 at the time of writing). I can't verify if this works, as their trial license approach is complex. Its also a ported .NET app: http://www.nsoftware.com/ipworks/zip/default.aspx

商业产品(撰写本文时为 500 美元)。我无法验证这是否有效,因为他们的试用许可方法很复杂。它也是一个移植的 .NET 应用程序:http: //www.nsoftware.com/ipworks/zip/default.aspx

A commercial product ($290 at the time of writing). Suitable only for Wnidows as it uses a dll: http://www.example-code.com/java/zip.asp

商业产品(撰写本文时为 290 美元)。仅适用于 Wnidows,因为它使用 dll:http: //www.example-code.com/java/zip.asp

回答by Oli

Is there a good (free) library for this?

是否有一个好的(免费)图书馆?

java.util.zip will do the zipping, but it won't do the passwords. And no, I don't know of any free ones that will. The cheapestI've seen is $150 for a developer seat.

java.util.zip 将进行压缩,但不会进行密码。不,我不知道有任何免费的会这样做。我见过的最便宜的开发者席位是 150 美元。

回答by PhiLho

7-Zip has the option to add a password in its command-line mode. Perhaps you can exec it to get this result (and it has a good compression ration too).

7-Zip 可以选择在其命令行模式下添加密码。也许你可以执行它来得到这个结果(它也有一个很好的压缩比)。

Drawbacks: external process, hard to make portable (even if 7-Zip is portable itself), not sure of distribution license.

缺点:外部进程,难以移植(即使 7-Zip 本身是可移植的),不确定分发许可证。

Note that InfoZip's Zip utility, highly portable too, also supports password.

请注意,InfoZip 的 Zip 实用程序也非常便携,也支持密码。

回答by Donal Tobin

If you give a better usage scenario then there are other alternatives.

如果您提供更好的使用场景,那么还有其他选择。

  1. Do you require the zip to be opened by the standard Zip tools that can handle a zip password?
  2. The same question as previous are you going to pass this zip to an external entity that has to open the zip?
  3. Is it internal only and you just want to protect the contents of the zip?
  1. 您是否需要使用可以处理 zip 密码的标准 Zip 工具打开 zip?
  2. 与之前相同的问题是您要将这个 zip 传递给必须打开 zip 的外部实体吗?
  3. 它是否仅限内部使用而您只想保护 zip 的内容?

For 3 then you can just use java to encrypt the stream contents of the zip as a normal file, probably best to change the file extension to .ezip or somesuch too.

对于 3,您可以只使用 java 将 zip 的流内容加密为普通文件,最好将文件扩展名更改为 .ezip 或类似的文件。

For 1 and 2 then you can use the chillkatsolution as mentioned, or an equivalent. However be aware that chillkat is not a pure Java solution, it uses JNI.

对于 1 和 2,您可以使用前面提到的chillkat解决方案或等效解决方案。但是请注意,chillkat 不是纯 Java 解决方案,它使用 JNI。

回答by PhiLho

Additional info: I googled a bit more and indeed, it is a quite common question, and it appears there is no free solution (yet?).

附加信息:我搜索了更多,确实,这是一个很常见的问题,而且似乎没有免费的解决方案(还没有?)。

Now, the standard algorithm of Zip encryption is well defined: See PKWARE's Application Noteon the .ZIP file format. It appears to be an encryption done on the encrypted stream. If somebody feels like coding it...

现在,Zip 加密的标准算法已经明确定义:请参阅 PKWARE 的关于 .ZIP 文件格式的应用说明。它似乎是对加密流进行的加密。如果有人想编码...

Now, I wonder why Sun didn't include it in its library? Lack of standard? Patent/legal issue? Too weak to be usable?

现在,我想知道为什么 Sun 没有将它包含在其库中?缺乏标准?专利/法律问题?太弱无法使用?

回答by Kevin Day

This isn't an answer, but it is a caution to keep in mind when evaluating potential solutions.

这不是答案,但在评估潜在解决方案时要牢记这一点。

One very important thing about zip encryption:

关于 zip 加密的一件非常重要的事情:

There are several types of zip encryption. The old type (part of the original zip standard) is not at all worth bothering with (it can be cracked in less than 10 minutes with apps easily available online).

有几种类型的 zip 加密。旧类型(原始 zip 标准的一部分)根本不值得打扰(使用在线轻松获得的应用程序可以在不到 10 分钟的时间内破解)。

If you are doing any sort of encryption of zip files, please, please be sure you use one of the strong encryption standards (I believe that WinZip's 128- and 256-bit AES standard is the best supported). Here are the technical specs- we used this when developing our own Java encrypted zip system (can't provide source - sorry - it's internal use only)

如果您正在对 zip 文件进行任何类型的加密,请确保您使用一种强大的加密标准(我相信 WinZip 的 128 位和 256 位 AES 标准是最受支持的)。这是技术规范- 我们在开发自己的 Java 加密 zip 系统时使用了它(无法提供源代码 - 抱歉 - 仅供内部使用)

The only thing worse than having no encryption is thinking that you have encryption and being wrong :-)

唯一比没有加密更糟糕的是认为你有加密并且是错误的:-)

回答by Kai Mechel

You can also try TrueZip. See the following links for features: https://christian-schlichtherle.bitbucket.io/truezip/

您也可以尝试 TrueZip。有关功能,请参阅以下链接:https: //christian-schlichtherle.bitbucket.io/truezip/

The successor of TrueZip can be found here: https://christian-schlichtherle.bitbucket.io/truevfs/

TrueZip 的继任者可以在这里找到:https://christian-schlichtherle.bitbucket.io/truevfs/

回答by Matt

You can try Zip4j, a pure java library to handle zip file. It supports encryption/ decryption of PKWare and AES encryption methods.

您可以尝试Zip4j,这是一个处理 zip 文件的纯 Java 库。它支持 PKWare 和 AES 加密方法的加密/解密。

Key features:

主要特点:

  • Create, Add, Extract, Update, Remove files from a Zip file
  • Read/Write password protected Zip files
  • Supports AES 128/256 Encryption
  • Supports Standard Zip Encryption
  • Supports Zip64 format
  • Supports Store (No Compression) and Deflate compression method
  • Create or extract files from Split Zip files (Ex: z01, z02,...zip)
  • Supports Unicode file names
  • Progress Monitor
  • 从 Zip 文件中创建、添加、提取、更新、删除文件
  • 读/写受密码保护的 Zip 文件
  • 支持 AES 128/256 加密
  • 支持标准 Zip 加密
  • 支持 Zip64 格式
  • 支持 Store (No Compression) 和 Deflate 压缩方法
  • 从 Split Zip 文件中创建或提取文件(例如:z01、z02、...zip)
  • 支持 Unicode 文件名
  • 进度监视器

License:

执照:

回答by Marc

Here's an example using winzipaes 1.0.1. Note this is just a gist, I have not tested this code in exactly this form.

这是一个使用 winzipaes 1.0.1 的示例。请注意,这只是一个要点,我还没有完全以这种形式测试此代码。

import de.idyl.winzipaes.AesZipFileEncrypter;
import de.idyl.winzipaes.impl.AESEncrypterBC;

File aNewZipFile = new File("/tmp/foo.zip");
File existingUnzippedFile = new File("/tmp/src.txt");

// We use the bouncy castle encrypter, as opposed to the JCA encrypter
AESEncrypterBC encrypter = new AESEncrypterBC();
encrypter.init("my-password", 0);  // The 0 is keySize, it is ignored for AESEncrypterBC

AesZipFileEncrypter zipEncrypter = new AesZipFileEncrypter(aNewZipFile, encrypter);
zipEncrypter.add(existingUnzippedFile, "src.txt", "my-password"); 

// remember to close the zipEncrypter
zipEncrypter.close();

You can them unzip "/tmp/foo.zip" using Winzip (v9+) or 7za (i.e. 7zip) on a Mac, using password "my-password".

您可以在 Mac 上使用 Winzip (v9+) 或 7za (即 7zip) 解压缩“/tmp/foo.zip”,使用密码“my-password”。

Note: it's not clear to me why it is necessary to specify the password twice in the code above. I do not know what would happen if you used different passwords in these two places.

注意:我不清楚为什么需要在上面的代码中指定两次密码。我不知道如果你在这两个地方使用不同的密码会发生什么。