Cung cấp các phương thức tĩnh cho việc tạo, trích dữ liệu và mở file dữ liệu zip.
ZipArchiveĐại diện cho tập các file được nén trong một file có định dạng ZIP.
ZipArchiveEntryĐại diện cho một tập tin nằm trong file định dạng ZIP.
DeflateStreamCung cấp các phương thức và thuộc tính cho các luồng (stream) nén và giải nén bằng cách sử dụng thuật toán Deflate.
GZipStreamCung cấp các phương thức và thuộc tính được sử dụng để nén và giải nén các luồng (stream).
Chú ý rằng các class này được đưa vào C# từ phiên bản 4.5, vì vậy project của bạn phải sử dụng .NET phiên bản 4.5 hoặc mới hơn.
2- ZipFileClass ZipFile là một class tiện ích, nó có nhiều phương thức tĩnh giúp bạn mở file zip, trích lấy dữ liệu, hoặc các tình huống hay được sử dụng như nén một thư mục thành một file zip, giải nén file zip ra một thư mục, ..
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO.Compression; namespace CompressionTutorial { class ZipDirectoryDemo { public static void Main(string[] args) { string inputDir = "C:/test/inputdir"; string zipPath = "C:/test/data.zip"; string extractPath = "C:/test/outputdir"; ZipFile.CreateFromDirectory(inputDir, zipPath); ZipFile.ExtractToDirectory(zipPath, extractPath); Console.WriteLine("Done!"); } } }Nếu bạn nhận được một thông báo lỗi: “The name ‘ZipFile’ does not exist in the current context” (Mặc dù đã khai báo using System.IO.Compression) điều đó có nghĩa là project của bạn sử dụng .NET cũ hơn 4.5 hoặc chương trình không tìm thấy thư viện DLL. Bạn có thể xem cách fix lỗi này trong phụ lục ở cuối của tài liệu này.
Chạy ví dụ và nhận được kết quả:
3- ZipArchiveZipArchive đại diện cho một bó các file đã nén trong một file định dạng ZIP. Bạn có thể tạo được đối tượng ZipArchive thông qua phương thức OpenRead của class ZipFile. Thông qua ZipArchive bạn có thể đọc các file con đã được nén trong file zip.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO.Compression; using chúng tôi namespace CompressionTutorial { class ListArchiveEntryDemo { public static void Main(string[] args) { string zipPath = "c:/test/data.zip"; using (ZipArchive archive = ZipFile.OpenRead(zipPath)) { { Console.WriteLine("Entry:"); } } Console.Read(); } } }Trích các file dữ liệu trong file zip:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO.Compression; using System.IO; namespace CompressionTutorial { class ExtractDemo { static void Main(string[] args) { string zipPath = "c:/test/data.zip"; string extractPath = "c:/test/extract"; if (!Directory.Exists(extractPath)) { System.IO.Directory.CreateDirectory(extractPath); } using (ZipArchive archive = ZipFile.OpenRead(zipPath)) { { { fileInfo.Directory.Create(); } } } Console.ReadLine(); } } }Bạn cũng có thể trèn thêm các file vào trong một file zip có sẵn.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO.Compression; using System.IO; namespace CompressionTutorial { class AddEntryDemo { static void Main(string[] args) { string zipPath = "C:/test/data.zip"; using (FileStream zipStream = new FileStream(zipPath, FileMode.Open)) { using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Update)) { ZipArchiveEntry readmeEntry = archive.CreateEntry("note/Note.txt"); using (StreamWriter writer = new StreamWriter(readmeEntry.Open())) { writer.WriteLine("## Note.txt"); writer.WriteLine("========================"); } } } } } }Chạy ví dụ và nhận được kết quả.
4- Phụ lục: Khắc phục lỗiKhi bạn nhận được lỗi: “The name ‘ZipFile’ does not exist in the current context” (Mặc dù đã khai báo using System.IO.Compression) điều đó có nghĩa là bạn đã sử dụng .NET cũ hơn phiên bản 4.5 hoặc chương trình không tìm được thư viện DLL cần thiết.
Nhấn phải chuột vào Project chọn Properties, đảm bảo rằng project của bạn đã sử dụng .NET Framework 4.5 trở lên.
Chạy lại class của bạn xem trình biên dịch còn thông báo lỗi đó nữa hay không. Trong trường hợp vẫn thông báo lỗi bạn cần chỉ định rõ vị trí thư viện DLL.
C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5System.IO.Compression.FileSystem.dll
C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5.1System.IO.Compression.FileSystem.dll
Nhấn phải chuột vào Project chọn:
chúng tôi
Tương tự nếu bạn nhận được thông báo “The name ‘ZipArchive’ does not exist in the current context” (Mặc dù đã khai báo using System.IO.Compression) bạn cần khai báo sử dụng thư viện chúng tôi :
Tương tự nếu bạn nhận được thông báo “The name ‘Path’ does not exist in the current context” (Mặc dù đã khai báo using chúng tôi bạn cần khai báo sử dụng thư viện chúng tôi :