65.9K
CodeProject 正在变化。 阅读更多。
Home

C# 和 Java 中的文件加密压缩

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.47/5 (9投票s)

2008年9月14日

CPOL

3分钟阅读

viewsIcon

76463

downloadIcon

5379

创建 C# 和 Java 中压缩、加密、受密码保护的 zip 文件的源代码

引言

本文提供了 C# 和 Java 中一个压缩框架的源代码。该库并不完整,但实现了创建 zip 文件所需的一切(可选压缩),其中包含多个可选密码加密的文件。

背景

我想了解 zip 文件的工作原理,并构建一个可以创建 zip 文件的库。我也喜欢比较和对比不同的编程语言。因此,我决定用 C# 和 Java 创建这个库。我打算将来用 C++ 实现这个库……但我知道已经有很多 C++ zip 库了。

Using the Code

只有两个主要的类值得关注

  • ZIPFile:这个对象代表一个 ZIP 文件,可以包含一个或多个其他文件(例如,文本文件、JPEG,任何文件(我想...)。
  • FileEntry:这个对象代表存储在 ZIP 文件中的一个文件。FileEntry 可以按原样存储,压缩,加密,压缩加密。这由创建 FileEntry 时调用的构造函数控制。

示例

创建一个包含一个文件的 ZIP 文件,使用存储(即无压缩)且无密码(即未加密)

using System;
using System.Collections.Generic;
using System.Text;
using ZipFramework; 

//Creates a ZipFile object that will ultimately be saved at D:\\myfirstZip.zip 
ZIPFile zip = new ZIPFile("D:\\myfirstZip.zip"); 

//Creates a FileEntry object that represents the file 
//D:\\text.txt
// the file will NOT be compressed or have a password 
FileEntry file = new FileEntry("D:\\text.txt",Method.Stored); 

//Add the FileEntry object to the ZIPFile object 
zip.AddFile(file); 
//Finally actually create the 
Zip file zip.CreateZIP(); 

创建一个包含一个文件的 ZIP 文件,使用 deflate 压缩且无密码

using System;
using System.Collections.Generic;
using System.Text;
using ZipFramework; 

//Creates a ZipFile object that will ultimately be saved at 
D:\\myfirstZip.zip 

ZIPFile zip = new ZIPFile("D:\\myfirstZip.zip"); 

 //Creates a FileEntry object that represents the file D:\\text.txt
 // the file will be compressed using the deflate algorithm 
 //(note this is the only algorithm the library supports) 
 FileEntry file = new FileEntry("D:\\text.txt",Method.Deflated); 

//Add the FileEntry object to the ZIPFile object 
 zip.AddFile(file); 

 //Finally actually create the Zip file 
zip.CreateZIP(); 

最终示例:创建一个包含一个文件的 ZIP 文件,使用 deflate 压缩并用密码加密

using System;
using System.Collections.Generic;
using System.Text;
using ZipFramework; 

//Creates a ZipFile object that will ultimately be saved at D:\\myfirstZip.zip 
ZIPFile zip = new ZIPFile("D:\\myfirstZip.zip"); 

//Creates a FileEntry object that represents the file 
//D:\\text.txt// the file will be compressed using the deflate algorithm 
//(note this is the only algorithm the library supports) and the file will 
//also be encrypted with the supplied password. 
FileEntry file = new FileEntry("D:\\text.txt", "HARPER",Method.Deflated); 

//Add the FileEntry object to the ZIPFile object 
zip.AddFile(file); 

//Finally actually create the Zip file 
zip.CreateZIP(); 

关注点

我学到了很多关于 C# 和 Java 之间差异的知识,特别是 C# 属性比 Java 的 getter 和 setter 编写起来容易得多。很长一段时间,C# 版本比 Java 版本快得多(例如,对于一个 16 兆加密文件,C# 需要 2-3 秒,Java 需要 2-3 分钟)。我最终将这个问题追溯到 IO,并且似乎在 C# 中,FileStream 对象会为你处理缓冲?而在 Java 中,你必须将流包装在缓冲流中。尽管如此,C# 版本仍然比 Java 版本略快(但这可能归因于我的编码,而不是 C# 和 Java 的主要差异)。

另一件让我无法解释的奇怪事情是,在使用 Deflate 压缩时,Java 代码在压缩数据方面表现更好(这让我担心,我期望 C# 和 Java 的 deflate 应该完全相同……但证据表明事实并非如此(如果有人可以解释这一点,请告诉我)。Java 不支持无符号数据类型,这真是一件痛苦的事情,而且我不是 100% 确定我的 Java 实现是万无一失的,因为这个原因。这个库真的只是足以输出一个可用的 zip 文件。如果有人想增强它(尤其是在我使用了非标准编码实践的地方),请这样做。另外,如果有人想添加解压缩功能,那将非常棒!!)

历史

  • 2008 年 9 月 14 日:第一个版本
© . All rights reserved.