/// <summary> /// Rewrite the directory within a zipfile. /// </summary> /// /// <remarks> /// /// <para> /// In cases of data error, the directory in a zip file can get out of /// synch with the entries in the zip file. This method attempts to fix /// the zip file if this has occurred. /// </para> /// /// <para> This can take a long time for large zip files. </para> /// /// <para> This won't work if the zip file uses a non-standard /// code page - neither IBM437 nor UTF-8. </para> /// /// <para> /// This method is not supported in the Reduced or Compact Framework /// versions of DotNetZip. /// </para> /// /// <para> /// Developers using COM can use the <see /// cref="ComHelper.FixZipDirectory(String)">ComHelper.FixZipDirectory(String)</see> /// method. /// </para> /// /// </remarks> /// /// <param name="zipFileName">The filename to of the zip file to fix.</param> /// /// <seealso cref="CheckZip(string)"/> /// <seealso cref="CheckZip(string,bool,System.IO.TextWriter)"/> internal static void FixZipDirectory(string zipFileName) { using (var zip = ZipFileExtensions.Read(zipFileName, new ReadOptions { FullScan = true })) { zip.Save(zipFileName); } }
/// <summary> /// Reads a zip file archive from the named filesystem file using the /// specified options. /// </summary> /// /// <remarks> /// <para> /// This version of the <c>Read()</c> method allows the caller to pass /// in a <c>TextWriter</c> an <c>Encoding</c>, via an instance of the /// <c>ReadOptions</c> class. The <c>ZipFile</c> is read in using the /// specified encoding for entries where UTF-8 encoding is not /// explicitly specified. /// </para> /// </remarks> /// /// <example> /// /// <para> /// 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. /// </para> /// /// <para> /// 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. /// </para> /// /// <code lang="C#"> /// 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); /// } /// } /// </code> /// /// /// <code lang="VB"> /// 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 /// </code> /// </example> /// /// /// <example> /// /// <para> /// 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 <c>StringWriter</c>. /// </para> /// /// <code lang="C#"> /// 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 /// </code> /// /// <code lang="VB"> /// 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 /// </code> /// </example> /// /// <exception cref="System.Exception"> /// Thrown if the zipfile cannot be read. The implementation of /// this method relies on <c>System.IO.File.OpenRead</c>, 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. /// </exception> /// /// <param name="zipFileName"> /// The name of the zip archive to open. /// This can be a fully-qualified or relative pathname. /// </param> /// /// <param name="options"> /// The set of options to use when reading the zip file. /// </param> /// /// <returns>The ZipFile instance read from the zip archive.</returns> /// /// <seealso cref="ZipFile.Read(Stream, ReadOptions)"/> /// public static ZipFile Read(string zipFileName, ReadOptions options) { return(ZipFileExtensions.Read(zipFileName, options)); }
/// <summary> /// Creates a new <c>ZipFile</c> instance, using the specified name for the /// filename, the specified status message writer, and the specified Encoding. /// </summary> /// /// <remarks> /// <para> /// This constructor works like the <see cref="ZipFile(String)">ZipFile /// constructor that accepts a single string argument.</see> See that /// reference for detail on what this constructor does. /// </para> /// /// <para> /// This version of the constructor allows the caller to pass in a /// <c>TextWriter</c>, and an Encoding. The <c>TextWriter</c> will collect /// verbose messages that are generated by the library during extraction or /// creation of the zip archive. A console application may wish to pass /// <c>System.Console.Out</c> to get messages on the Console. A graphical or /// headless application may wish to capture the messages in a different /// <c>TextWriter</c>, for example, a <c>StringWriter</c>, and then display /// the messages in a <c>TextBox</c>, or generate an audit log of /// <c>ZipFile</c> operations. /// </para> /// /// <para> /// The <c>Encoding</c> is used as the default alternate encoding for entries /// with filenames or comments that cannot be encoded with the IBM437 code /// page. This is a equivalent to setting the <see /// cref="ProvisionalAlternateEncoding"/> property on the <c>ZipFile</c> /// instance after construction. /// </para> /// /// <para> /// To encrypt the data for the files added to the <c>ZipFile</c> instance, /// set the <c>Password</c> property after creating the <c>ZipFile</c> /// instance. /// </para> /// /// <para> /// Instances of the <c>ZipFile</c> class are not multi-thread safe. You may /// not party on a single instance with multiple threads. You may have /// multiple threads that each use a distinct <c>ZipFile</c> instance, or you /// can synchronize multi-thread access to a single instance. /// </para> /// /// </remarks> /// /// <exception cref="Ionic.Zip.ZipException"> /// Thrown if <c>fileName</c> refers to an existing file that is not a valid zip file. /// </exception> /// /// <param name="zipFileName">The filename to use for the new zip archive.</param> /// <param name="statusMessageWriter">A TextWriter to use for writing verbose /// status messages.</param> /// <param name="encoding"> /// The Encoding is used as the default alternate encoding for entries with /// filenames or comments that cannot be encoded with the IBM437 code page. /// </param> public static ZipFile Read(string zipFileName, TextWriter statusMessageWriter, System.Text.Encoding encoding) { return(ZipFileExtensions.Read(zipFileName, statusMessageWriter, encoding)); }
/// <summary> /// Reads a zip file archive using the specified the specified TextWriter for /// status messages and returns the instance. /// </summary> /// /// <param name="zipFileName"> /// The name of the zip archive to open. /// This can be a fully-qualified or relative pathname. /// </param> /// /// <param name="statusMessageWriter"> /// The <c>System.IO.TextWriter</c> to use for writing verbose status messages /// during operations on the zip archive. A console application may wish to /// pass <c>System.Console.Out</c> to get messages on the Console. A graphical /// or headless application may wish to capture the messages in a different /// <c>TextWriter</c>, such as a <c>System.IO.StringWriter</c>. /// </param> /// /// <returns>The instance read from the zip archive.</returns> /// public static ZipFile Read(string zipFileName, TextWriter statusMessageWriter) { return(ZipFileExtensions.Read(zipFileName, statusMessageWriter)); }
/// <summary> /// Reads a zip file archive using the specified text encoding and returns the /// instance. /// </summary> /// /// <param name="zipFileName"> /// The name of the zip archive to open. /// This can be a fully-qualified or relative pathname. /// </param> /// /// <param name="encoding"> /// The <c>System.Text.Encoding</c> to use when reading in the zip archive. Be /// careful specifying the encoding. If the value you use here is not the same /// as the Encoding used when the zip archive was created (possibly by a /// different archiver) you will get unexpected results and possibly exceptions. /// </param> /// /// <returns>The instance read from the zip archive.</returns> /// public static ZipFile Read(string zipFileName, System.Text.Encoding encoding) { return(ZipFileExtensions.Read(zipFileName, encoding)); }
/// <summary> /// Reads a zip file archive and returns the instance. /// </summary> /// /// <remarks> /// <para> /// The stream is read using the default <c>System.Text.Encoding</c>, which is the /// <c>IBM437</c> codepage. /// </para> /// </remarks> /// /// <exception cref="System.Exception"> /// Thrown if the <c>ZipFile</c> cannot be read. The implementation of this method /// relies on <c>System.IO.File.OpenRead</c>, 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. /// </exception> /// /// <param name="zipFileName"> /// The name of the zip archive to open. This can be a fully-qualified or relative /// pathname. /// </param> /// /// <seealso cref="ZipFile.Read(String, ReadOptions)"/>. /// /// <returns>The instance read from the zip archive.</returns> /// public static ZipFile Read(string zipFileName) { return(ZipFileExtensions.Read(zipFileName)); }