C# | Visual Basic | Visual C++ |
public static ZipFile Read( string fileName, ReadOptions options )
Public Shared Function Read ( _ fileName As String, _ options As ReadOptions _ ) As ZipFile
public: static ZipFile^ Read( String^ fileName, ReadOptions^ options )
- fileName (String)
- The name of the zip archive to open. This can be a fully-qualified or relative pathname.
- options (ReadOptions)
- The set of options to use when reading the zip file.
This version of the Read() method allows the caller to pass in a TextWriter an Encoding, via an instance of the ReadOptions class. The ZipFile is read in using the specified encoding for entries where UTF-8 encoding is not explicitly specified.
This example shows how to read a zip file using the Big-5 Chinese code page (950), and extract each entry in the zip file, while sending status messages out to the Console.
For this code to work as intended, the zipfile must have been created using the big5 code page (CP950). This is typical, for example, when using WinRar on a machine with CP950 set as the default code page. In that case, the names of entries within the Zip archive will be stored in that code page, and reading the zip archive must be done using that code page. If the application did not use the correct code page in ZipFile.Read(), then names of entries within the zip archive would not be correctly retrieved.
string zipToExtract = "MyArchive.zip"; string extractDirectory = "extract"; var options = new ReadOptions { StatusMessageWriter = System.Console.Out, Encoding = System.Text.Encoding.GetEncoding(950) }; using (ZipFile zip = ZipFile.Read(zipToExtract, options)) { foreach (ZipEntry e in zip) { e.Extract(extractDirectory); } }
Dim zipToExtract as String = "MyArchive.zip" Dim extractDirectory as String = "extract" Dim options as New ReadOptions options.Encoding = System.Text.Encoding.GetEncoding(950) options.StatusMessageWriter = System.Console.Out Using zip As ZipFile = ZipFile.Read(zipToExtract, options) Dim e As ZipEntry For Each e In zip e.Extract(extractDirectory) Next End Using
This example shows how to read a zip file using the default code page, to remove entries that have a modified date before a given threshold, sending status messages out to a StringWriter.
var options = new ReadOptions { StatusMessageWriter = new System.IO.StringWriter() }; using (ZipFile zip = ZipFile.Read("PackedDocuments.zip", options)) { var Threshold = new DateTime(2007,7,4); // We cannot remove the entry from the list, within the context of // an enumeration of said list. // So we add the doomed entry to a list to be removed later. // pass 1: mark the entries for removal var MarkedEntries = new System.Collections.Generic.List<ZipEntry>(); foreach (ZipEntry e in zip) { if (e.LastModified < Threshold) MarkedEntries.Add(e); } // pass 2: actually remove the entry. foreach (ZipEntry zombie in MarkedEntries) zip.RemoveEntry(zombie); zip.Comment = "This archive has been updated."; zip.Save(); } // can now use contents of sw, eg store in an audit log
Dim options as New ReadOptions options.StatusMessageWriter = New System.IO.StringWriter Using zip As ZipFile = ZipFile.Read("PackedDocuments.zip", options) Dim Threshold As New DateTime(2007, 7, 4) ' We cannot remove the entry from the list, within the context of ' an enumeration of said list. ' So we add the doomed entry to a list to be removed later. ' pass 1: mark the entries for removal Dim MarkedEntries As New System.Collections.Generic.List(Of ZipEntry) Dim e As ZipEntry For Each e In zip If (e.LastModified < Threshold) Then MarkedEntries.Add(e) End If Next ' pass 2: actually remove the entry. Dim zombie As ZipEntry For Each zombie In MarkedEntries zip.RemoveEntry(zombie) Next zip.Comment = "This archive has been updated." zip.Save End Using ' can now use contents of sw, eg store in an audit log
Exception | Condition |
---|---|
Exception |
Thrown if the zipfile cannot be read. The implementation of
this method relies on System.IO.File.OpenRead, which
can throw a variety of exceptions, including specific
exceptions if a file is not found, an unauthorized access
exception, exceptions for poorly formatted filenames, and so
on.
|