text stringlengths 0 13.4k |
|---|
Description |
IOException |
The file already exists or the source file could not be found. |
ArgumentNullException |
The value of the Source and/or Destination parameters is null. |
ArgumentException |
The value of the Source and/or Destination parameters are empty, or contain |
invalid characters. |
UnauthorizedAccessException You do not have the required permissions in order to perform this action. |
PathTooLongException |
The Source, Destination or specified path(s) exceed the maximum length. On |
Windows, a Path's length must be less than 248 characters, while File names must |
be less than 260 characters. |
DirectoryNotFoundException The specified directory could not be found. |
NotSupportedException |
The Source or Destination paths or file names are in an invalid format. |
GoalKicker.com – .NET Framework Notes for Professionals |
75 |
Chapter 19: Reading and writing Zip files |
The ZipFile class lives in the System.IO.Compression namespace. It can be used to read from, and write to Zip |
files. |
Section 19.1: Listing ZIP contents |
This snippet will list all the filenames of a zip archive. The filenames are relative to the zip root. |
using (FileStream fs = new FileStream("archive.zip", FileMode.Open)) |
using (ZipArchive archive = new ZipArchive(fs, ZipArchiveMode.Read)) |
{ |
for (int i = 0; i < archive.Entries.Count; i++) |
{ |
Console.WriteLine($"{i}: {archive.Entries[i]}"); |
} |
} |
Section 19.2: Extracting files from ZIP files |
Extracting all the files into a directory is very easy: |
using (FileStream fs = new FileStream("archive.zip", FileMode.Open)) |
using (ZipArchive archive = new ZipArchive(fs, ZipArchiveMode.Read)) |
{ |
archive.ExtractToDirectory(AppDomain.CurrentDomain.BaseDirectory); |
} |
When the file already exists, a System.IO.IOException will be thrown. |
Extracting specific files: |
using (FileStream fs = new FileStream("archive.zip", FileMode.Open)) |
using (ZipArchive archive = new ZipArchive(fs, ZipArchiveMode.Read)) |
{ |
// Get a root entry file |
archive.GetEntry("test.txt").ExtractToFile("test_extracted_getentries.txt", true); |
// Enter a path if you want to extract files from a subdirectory |
archive.GetEntry("sub/subtest.txt").ExtractToFile("test_sub.txt", true); |
// You can also use the Entries property to find files |
archive.Entries.FirstOrDefault(f => f.Name == |
"test.txt")?.ExtractToFile("test_extracted_linq.txt", true); |
// This will throw a System.ArgumentNullException because the file cannot be found |
archive.GetEntry("nonexistingfile.txt").ExtractToFile("fail.txt", true); |
} |
Any of these methods will produce the same result. |
Section 19.3: Updating a ZIP file |
To update a ZIP file, the file has to be opened with ZipArchiveMode.Update instead. |
using (FileStream fs = new FileStream("archive.zip", FileMode.Open)) |
GoalKicker.com – .NET Framework Notes for Professionals |
76 |
using (ZipArchive archive = new ZipArchive(fs, ZipArchiveMode.Update)) |
{ |
// Add file to root |
archive.CreateEntryFromFile("test.txt", "test.txt"); |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.