C#.NET: Compress/Decompress zip file using System.IO.Packaging
.NET 中常见的 ZIP 操作类库: SharpZipLib,J# Zip Library,zLib.NET。
.NET 3.0 开始,新增了 System.IO.Packaging.ZipPackage,从此再不用第三方的Lib了。
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.IO.Packaging;
5
6 public class SharpZip
7 {
8 private const long BUFFER_SIZE = 4096;
9
10 public static void CompressFiles(List<string> fileNames, string zipFileName)
11 {
12 foreach (string file in fileNames)
13 {
14 CompressFile(zipFileName, file);
15 }
16 }
17
18 public static void CompressFile(string zipFilename, string fileToAdd)
19 {
20 using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate))
21 {
22 string destFilename = ".\\" + Path.GetFileName(fileToAdd);
23 Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
24 if (zip.PartExists(uri))
25 {
26 zip.DeletePart(uri);
27 }
28 PackagePart part = zip.CreatePart(uri, "", CompressionOption.Normal);
29 using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read))
30 {
31 using (Stream dest = part.GetStream())
32 {
33 CopyStream(fileStream, dest);
34 }
35 }
36 }
37 }
38
39 public static void DecompressFile(string zipFilename, string outPath)
40 {
41 using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.Open))
42 {
43 foreach (PackagePart part in zip.GetParts())
44 {
45 string outFileName = Path.Combine(outPath, part.Uri.OriginalString.Substring(1));
46 using (System.IO.FileStream outFileStream = new System.IO.FileStream(outFileName, FileMode.Create))
47 {
48 using (Stream inFileStream = part.GetStream())
49 {
50 CopyStream(inFileStream, outFileStream);
51 }
52 }
53 }
54 }
55 }
56
57 private static void CopyStream(System.IO.Stream inputStream, System.IO.Stream outputStream)
58 {
59 long bufferSize = inputStream.Length < BUFFER_SIZE ? inputStream.Length : BUFFER_SIZE;
60 byte[] buffer = new byte[bufferSize];
61 int bytesRead = 0;
62 long bytesWritten = 0;
63 while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) != 0)
64 {
65 outputStream.Write(buffer, 0, bytesRead);
66 bytesWritten += bufferSize;
67 }
68 }
69 }
2 using System.Collections.Generic;
3 using System.IO;
4 using System.IO.Packaging;
5
6 public class SharpZip
7 {
8 private const long BUFFER_SIZE = 4096;
9
10 public static void CompressFiles(List<string> fileNames, string zipFileName)
11 {
12 foreach (string file in fileNames)
13 {
14 CompressFile(zipFileName, file);
15 }
16 }
17
18 public static void CompressFile(string zipFilename, string fileToAdd)
19 {
20 using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate))
21 {
22 string destFilename = ".\\" + Path.GetFileName(fileToAdd);
23 Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
24 if (zip.PartExists(uri))
25 {
26 zip.DeletePart(uri);
27 }
28 PackagePart part = zip.CreatePart(uri, "", CompressionOption.Normal);
29 using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read))
30 {
31 using (Stream dest = part.GetStream())
32 {
33 CopyStream(fileStream, dest);
34 }
35 }
36 }
37 }
38
39 public static void DecompressFile(string zipFilename, string outPath)
40 {
41 using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.Open))
42 {
43 foreach (PackagePart part in zip.GetParts())
44 {
45 string outFileName = Path.Combine(outPath, part.Uri.OriginalString.Substring(1));
46 using (System.IO.FileStream outFileStream = new System.IO.FileStream(outFileName, FileMode.Create))
47 {
48 using (Stream inFileStream = part.GetStream())
49 {
50 CopyStream(inFileStream, outFileStream);
51 }
52 }
53 }
54 }
55 }
56
57 private static void CopyStream(System.IO.Stream inputStream, System.IO.Stream outputStream)
58 {
59 long bufferSize = inputStream.Length < BUFFER_SIZE ? inputStream.Length : BUFFER_SIZE;
60 byte[] buffer = new byte[bufferSize];
61 int bytesRead = 0;
62 long bytesWritten = 0;
63 while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) != 0)
64 {
65 outputStream.Write(buffer, 0, bytesRead);
66 bytesWritten += bufferSize;
67 }
68 }
69 }