小猪最近在使用SharpZipLib进行zip包的操作,编写了下列测试代码。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51static void Main(string[] args)
{
Console.WriteLine("---------------------Zip包中的文件并解压测试-------------------------");
string content;
string filename = @"E:wamp.zip";//需要测试的zip包地址
string searchname = @"Web.config";
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite))
using (ZipFile zf = new ZipFile(fs))
{
Console.WriteLine("开始查找:" + searchname + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
Console.WriteLine("Zip包大小:" + fs.Length);
var ze = zf.GetEntry(searchname);
if (ze == null)
{
}
else
{
Console.WriteLine("文件长度:" + ze.Size);
Console.WriteLine("开始修改:" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
using (Stream s = zf.GetInputStream(ze))
{
StreamReader inputReader = new StreamReader(s);
content = inputReader.ReadToEnd\(\);
content += "nthis is SmallerPig Test 测试下中文";//在原流中增加字符串
}
zf.BeginUpdate\(\);
zf.Add(new StateDataSource(content), searchname);
zf.CommitUpdate\(\);
Console.WriteLine("结束:" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
}
}
}
class StateDataSource:IStaticDataSource//自定义实现IStaticDataSource接口的类
{
string source;
public StateDataSource(string source)
{
this.source = source;
}
public Stream GetSource\(\)
{
byte[] array = Encoding.ASCII.GetBytes(source);
MemoryStream stream = new MemoryStream(array);
return stream;
}
}
由测试结果知道:编辑一个zip中文件所需时间主要是由zip的大小来决定的,整个提交过程需要重新打包。