随笔一 ZIP

pdf=>zip
 1 using System;
 2 using System.IO;
 3 using ICSharpCode.SharpZipLib.Checksums;
 4 using ICSharpCode.SharpZipLib.Zip;
 5 
 6 //引用"ICSharpCode.SharpZipLib.dll"
 7 void ConvertToZIP()
 8 {
 9             Crc32 crc32 = new Crc32();
10 
11             byte[] buffer;
12             using (FileStream fs = File.OpenRead(@"d:\\oldnameXXXX.pdf"))
13             {
14                 buffer = new byte[fs.Length];
15                 fs.Read(buffer, 0, (int) fs.Length);
16             }
17 
18             byte[] outBuffer;
19             using (MemoryStream ms = new MemoryStream())
20             {
21                 ZipOutputStream zipOutputStream = new ZipOutputStream(ms);
22                 zipOutputStream.SetLevel(5);
23 
24                 ZipEntry zipEntry = new ZipEntry("NewNameXXXX.pdf");
25                 zipEntry.DateTime = DateTime.Now;
26                 zipEntry.Size = buffer.Length;
27                 crc32.Reset();
28                 crc32.Update(buffer);
29                 zipEntry.Crc = crc32.Value;
30 
31                 zipOutputStream.PutNextEntry(zipEntry);
32                 zipOutputStream.Write(buffer,0,buffer.Length);
33                 zipOutputStream.Finish();
34                 zipOutputStream.Close();
35 
36                 outBuffer = ms.ToArray();
37             }
38 
39             using (FileStream fs = new FileStream("D:\\XXXX.ZIP",FileMode.Create))
40             {
41                 fs.Write(outBuffer,0,outBuffer.Length);
42                 fs.Flush();
43             }
44 }

你可能感兴趣的:(随笔一 ZIP)