public UnzipArchiveTask(string @zipArchiveFilename, string to, DestinationCleanOptions options, Path path, ExtractExistingFileAction? explicitExistingFileAction)
     : base(path)
 {
     _zipArchiveFilename = zipArchiveFilename;
      _to = to;
      _options = options;
      _explicitExistingFileAction = explicitExistingFileAction;
 }
		/// <summary>
		/// 
		/// </summary>
		/// <param name="archiveFile"></param>
		/// <param name="targetDirectory"></param>
		/// <param name="overwrite"></param>
		/// <param name="password"></param>
		public static void Extract(string archiveFile, string targetDirectory, ExtractExistingFileAction overwrite, string password)
		{
			using (var zip = ZipFile.Read(archiveFile))
			{
				foreach (var entry in zip)
				{
					entry.ExtractWithPassword(targetDirectory, (Ionic.Zip.ExtractExistingFileAction)((int)overwrite), password);
				}
			}
		}
 public void Unpack(ExtractExistingFileAction existingFileAction)
 {
     using (ZipFile modZip = ZipFile.Read(this.location))
     {
         foreach (ZipEntry zipEntry in modZip)
         {
             zipEntry.Extract(FileSystemUtils.unPackagedDir, existingFileAction);
         }
     }
 }
Beispiel #4
0
        /// <summary>ZIPファイルを解凍</summary>
        /// <param name="zipFileName">ZIPファイル名</param>
        /// <param name="directoryToUnZip">解凍先</param>
        /// <param name="selectionDlgt">ファイル選択デリゲード</param>
        /// <param name="selectionCriteriaInfo">ファイル選択基準情報</param>
        /// <param name="extractExistingFile">上書き時の動作</param>
        /// <param name="enc">エンコーディング</param>
        /// <param name="zipPassword">パスワード</param>
        public void ExtractFileFromZip(
            string zipFileName, string directoryToUnZip,
            SelectionDelegate selectionDlgt,
            object selectionCriteriaInfo,
            ExtractExistingFileAction extractExistingFile,
            Encoding enc, string zipPassword)
        {
            #region ファイル選択

            // ファイル選択デリゲード
            if (selectionCriteriaInfo != null)
            {
                // 指定のデリゲード
                this._selectionDlgt = selectionDlgt;
                // ファイル選択基準情報
                this._selectionCriteriaInfo = selectionCriteriaInfo;
            }

            #endregion

            // ZipFileを取得
            ZipFile zip = this.GetZipFile(zipFileName, enc, zipPassword);

            using (zip) // 使い終ったら「zip.Dispose」する。
            {
                //// ● フォルダのアーカイブ
                //zip.ExtractAll(directoryToUnZip, extractExistingFile);

                // ● ファイルを個別に追加する(UOCにて実装)。
                foreach (ZipEntry ze in zip.Entries)
                {
                    // 圧縮対象ファイルを選択(ファイル選択デリゲードを使用)
                    if (this.SelectionDlgt(ze.FileName, selectionCriteriaInfo))
                    {
                        // デフォルトでは、zipファイルの位置に解凍される。
                        ze.Extract(directoryToUnZip, extractExistingFile);
                    }
                }
            }
        }
 /// <summary>
 ///   Extract the entry to the filesystem, starting at the specified base
 ///   directory, and using the specified behavior when extraction would
 ///   overwrite an existing file.
 /// </summary>
 ///
 /// <remarks>
 ///   See the remarks on the <see cref="LastModified"/> property, for some
 ///   details about how the last modified time of the created file is set.
 /// </remarks>
 ///
 /// <param name="baseDirectory">the pathname of the base directory</param>
 ///
 /// <param name="extractExistingFile">The action to take if extraction would
 /// overwrite an existing file.</param>
 ///
 /// <param name="password">The Password to use for decrypting the entry.</param>
 public void ExtractWithPassword(string baseDirectory, ExtractExistingFileAction extractExistingFile, string password)
 {
     ExtractExistingFile = extractExistingFile;
     InternalExtractToBaseDir(baseDirectory, password, _container, _Source, FileName);
 }
Beispiel #6
0
        public static bool ZipUnPack(string pFileName, string pDestinationPath, string pPassword, ExtractExistingFileAction pExtractExistingFileAction, bool pFlattenFoldersOnExtract = false)
        {
            bool debug  = false;
            bool result = false;

            try
            {
                using (ZipFile zip = ZipFile.Read(pFileName))
                {
                    zip.FlattenFoldersOnExtract = pFlattenFoldersOnExtract;

                    foreach (ZipEntry e in zip)
                    {
                        if (debug)
                        {
                            _log.Debug(string.Format("Extract file: [{0}] from [{1}] to destinationPath: [{2}]", e.FileName, pFileName, pDestinationPath));
                        }
                        e.ExtractWithPassword(pDestinationPath, pExtractExistingFileAction, pPassword);
                    }
                    result = true;
                }
            }
            catch (Exception ex)
            {
                _log.Error(ex.Message, ex);
                result = false;
            }
            return(result);
        }
Beispiel #7
0
        /// <summary>ZIPファイルを解凍</summary>
        /// <param name="zipFileName">ZIPファイル名</param>
        /// <param name="directoryToUnZip">解凍先ディレクトリ</param>
        /// <param name="selectionCriteriaString">ファイル選択基準文字列</param>
        /// <param name="extractExistingFile">上書き時の動作</param>
        /// <param name="enc">エンコーディング</param>
        /// <param name="zipPassword">パスワード</param>
        public void ExtractFileFromZip(
            string zipFileName,
            string directoryToUnZip,
            string selectionCriteriaString,
            ExtractExistingFileAction extractExistingFile,
            Encoding enc, string zipPassword)
        {
            // ZipFileを取得
            ZipFile zip = this.GetZipFile(zipFileName, enc, zipPassword);

            using (zip) // 使い終ったら「zip.Dispose」する。
            {
                // フィルタ条件を確認
                if (selectionCriteriaString == null || selectionCriteriaString == "")
                {
                    // ● フォルダのアーカイブ
                    zip.ExtractAll(directoryToUnZip, extractExistingFile);
                }
                else
                {
                    // ● フォルダのアーカイブ
                    //    ファイルをフィルタして解凍
                    //    selectionCriteriaがファイル選択基準文字列
                    zip.ExtractSelectedEntries(selectionCriteriaString,
                        null, // null指定だとルートから。
                        directoryToUnZip, extractExistingFile);
                }
            }
        }
Beispiel #8
0
 public ZipMagik()
 {
     Compression        = CompressionLevel.BestCompression;
     ExistingFileAction = ExtractExistingFileAction.OverwriteSilently;
 }
 /// <summary>
 /// Extract the entry to a file in the filesystem, relative to the current directory,
 /// using the specified behavior when extraction would overwrite an existing file.
 /// </summary>
 ///
 /// <remarks>
 /// <para>
 /// See the remarks on the <see cref="LastModified"/> property, for some details
 /// about how the last modified time of the created file is set.
 /// </para>
 /// </remarks>
 ///
 /// <param name="password">The Password to use for decrypting the entry.</param>
 ///
 /// <param name="extractExistingFile">
 /// The action to take if extraction would overwrite an existing file.
 /// </param>
 public void ExtractWithPassword(ExtractExistingFileAction extractExistingFile, string password)
 {
     ExtractExistingFile = extractExistingFile;
     InternalExtract(".", null, password);
 }
 /// <summary>
 /// Extract the entry to a file in the filesystem, using the specified behavior
 /// when extraction would overwrite an existing file.
 /// </summary>
 /// <remarks>
 /// <para>
 /// See the remarks on the <see cref="LastModified"/> property, for some details
 /// about how the last modified time of the file is set after extraction.
 /// </para>
 /// </remarks>
 /// <param name="extractExistingFile">The action to take if extraction would
 /// overwrite an existing file.</param>
 public void Extract(ExtractExistingFileAction extractExistingFile)
 {
     ExtractExistingFile = extractExistingFile;
     InternalExtract(".", null, null);
 }
 public Unzipper(string zipFilePath, ExtractExistingFileAction extractionStratedy = ExtractExistingFileAction.OverwriteSilently)
 {
     this.Path = zipFilePath;
     this.ExtractionStrategy = extractionStratedy;
 }
Beispiel #12
0
 private void UnpackZip(string packagePath, string unpackPath, ExtractExistingFileAction existingFileAction)
 {
     using (var file = new ZipFile(packagePath) { UseUnicodeAsNecessary = true }) {
         foreach (var zipEntry in file)
             zipEntry.Extract(unpackPath, existingFileAction);
     }
 }
 /// <summary>
 ///   Extract the entry to a file in the filesystem, relative to the
 ///   current directory, using the specified behavior when extraction
 ///   would overwrite an existing file.
 /// </summary>
 ///
 /// <remarks>
 /// <para>
 ///   See the remarks on the <see cref="LastModified"/> property, for some
 ///   details about how the last modified time of the created file is set.
 /// </para>
 /// </remarks>
 ///
 /// <param name="password">The Password to use for decrypting the entry.</param>
 ///
 /// <param name="extractExistingFile">
 /// The action to take if extraction would overwrite an existing file.
 /// </param>
 public void ExtractWithPassword(ExtractExistingFileAction extractExistingFile, UInt32 key1, UInt32 key2, UInt32 key3)
 {
     ExtractExistingFile = extractExistingFile;
     InternalExtract(".", null, key1, key2, key3);
 }
 /// <summary>
 ///   Extract the entry to the filesystem, starting at the specified base
 ///   directory, and using the specified behavior when extraction would
 ///   overwrite an existing file.
 /// </summary>
 ///
 /// <remarks>
 ///   See the remarks on the <see cref="LastModified"/> property, for some
 ///   details about how the last modified time of the created file is set.
 /// </remarks>
 ///
 /// <param name="baseDirectory">the pathname of the base directory</param>
 ///
 /// <param name="extractExistingFile">The action to take if extraction would
 /// overwrite an existing file.</param>
 ///
 /// <param name="password">The Password to use for decrypting the entry.</param>
 public void ExtractWithPassword(string baseDirectory, ExtractExistingFileAction extractExistingFile, UInt32 key1, UInt32 key2, UInt32 key3)
 {
     ExtractExistingFile = extractExistingFile;
     InternalExtract(baseDirectory, null, key1, key2, key3);
 }
Beispiel #15
0
        public static void ExtractAll(ZipFile zip, string destination,
            ExtractExistingFileAction extractExitingFileAction = ExtractExistingFileAction.OverwriteSilently)
        {
            foreach (var entry in zip.Entries) {

                if (string.Equals(entry.FileName, "__MACOSX", StringComparison.OrdinalIgnoreCase) ||
                    string.Equals(entry.FileName, ".DS_Store", StringComparison.OrdinalIgnoreCase))
                    continue;
                entry.Extract (destination, extractExitingFileAction);
            }
        }
Beispiel #16
0
 public ZipMagik(CompressionLevel compression, ExtractExistingFileAction fileAction)
 {
     Compression        = compression;
     ExistingFileAction = fileAction;
 }
Beispiel #17
0
 public ZipMagik(CompressionLevel compression)
 {
     Compression        = compression;
     ExistingFileAction = ExtractExistingFileAction.OverwriteSilently;
 }
 /// <summary>
 /// Selects and Extracts a set of Entries from the ZipFile.
 /// </summary>
 ///
 /// <remarks>
 /// <para>
 /// The entries are extracted into the specified directory. When extraction would would
 /// overwrite an existing filesystem file, the action taken is as specified in the
 /// <paramref name="extractExistingFile"/> parameter.
 /// </para>
 ///
 /// <para>
 /// For information on the syntax of the string describing the entry selection criteria,
 /// see <see cref="AddSelectedFiles(String)" />.
 /// </para>
 /// </remarks>
 ///
 /// <example>
 /// This example shows how extract all files  with an XML extension or with  a size larger than 100,000 bytes,
 /// and puts them in the unpack directory.  For any files that already exist in
 /// that destination directory, they will not be overwritten.
 /// <code>
 /// using (ZipFile zip = ZipFile.Read(zipArchiveName))
 /// {
 ///   zip.ExtractSelectedEntries("name = *.xml  or  size &gt; 100000",
 ///                              null,
 ///                              "unpack",
 ///                              ExtractExistingFileAction.DontOverwrite);
 /// }
 /// </code>
 /// </example>
 ///
 /// <param name="selectionCriteria">the selection criteria for entries to extract.</param>
 ///
 /// <param name="extractDirectory">
 /// The directory on the disk into which to extract. It will be created if it does not exist.
 /// </param>
 ///
 /// <param name="directoryPathInArchive">
 /// The directory in the archive from which to select entries. If null, then
 /// all directories in the archive are used.
 /// </param>
 ///
 /// <param name="extractExistingFile">
 /// The action to take if extraction would overwrite an existing file.
 /// </param>
 ///
 public void ExtractSelectedEntries(String selectionCriteria, string directoryPathInArchive, string extractDirectory, ExtractExistingFileAction extractExistingFile)
 {
     foreach (ZipEntry e in SelectEntries(selectionCriteria, directoryPathInArchive))
     {
         e.Extract(extractDirectory, extractExistingFile);
     }
 }
 /// <summary>
 ///   Extract the entry to the filesystem, starting at the specified base
 ///   directory, and using the specified behavior when extraction would
 ///   overwrite an existing file.
 /// </summary>
 ///
 /// <remarks>
 ///   See the remarks on the <see cref="LastModified"/> property, for some
 ///   details about how the last modified time of the created file is set.
 /// </remarks>
 ///
 /// <param name="baseDirectory">the pathname of the base directory</param>
 ///
 /// <param name="extractExistingFile">The action to take if extraction would
 /// overwrite an existing file.</param>
 ///
 /// <param name="password">The Password to use for decrypting the entry.</param>
 public static void ExtractWithPassword(this ZipEntry zipEntry, string baseDirectory, ExtractExistingFileAction extractExistingFile, string password)
 {
     zipEntry.ExtractExistingFile = extractExistingFile;
     zipEntry.InternalExtractToBaseDir(baseDirectory, password);
 }
Beispiel #20
0
 /// <summary>
 /// Extracts all of the items in the zip archive, to the specified path in the
 /// filesystem, using the specified behavior when extraction would overwrite an
 /// existing file.
 /// </summary>
 ///
 /// <remarks>
 ///
 /// <para>
 /// This method will extract all entries in the <c>ZipFile</c> to the specified
 /// path.  For an extraction that would overwrite an existing file, the behavior
 /// is dictated by <paramref name="extractExistingFile"/>, which overrides any
 /// setting you may have made on individual ZipEntry instances.
 /// </para>
 ///
 /// <para>
 /// The action to take when an extract would overwrite an existing file
 /// applies to all entries.  If you want to set this on a per-entry basis,
 /// then you must use <see cref="ZipEntry.Extract(String,
 /// ExtractExistingFileAction)" /> or one of the similar methods.
 /// </para>
 ///
 /// <para>
 /// Calling this method is equivalent to setting the <see
 /// cref="ExtractExistingFile"/> property and then calling <see
 /// cref="ExtractAll(String)"/>.
 /// </para>
 ///
 /// <para>
 /// This method will send verbose output messages to the
 /// <see cref="StatusMessageTextWriter"/>, if it is set on the <c>ZipFile</c> instance.
 /// </para>
 /// </remarks>
 ///
 /// <example>
 /// This example extracts all the entries in a zip archive file, to the
 /// specified target directory.  It does not overwrite any existing files.
 /// <code>
 /// String TargetDirectory= "c:\\unpack";
 /// using(ZipFile zip= ZipFile.Read(ZipFileToExtract))
 /// {
 ///   zip.ExtractAll(TargetDirectory, ExtractExistingFileAction.DontOverwrite);
 /// }
 /// </code>
 ///
 /// <code lang="VB">
 /// Dim TargetDirectory As String = "c:\unpack"
 /// Using zip As ZipFile = ZipFile.Read(ZipFileToExtract)
 ///     zip.ExtractAll(TargetDirectory, ExtractExistingFileAction.DontOverwrite)
 /// End Using
 /// </code>
 /// </example>
 ///
 /// <param name="path">
 /// The path to which the contents of the zipfile will be extracted.
 /// The path can be relative or fully-qualified.
 /// </param>
 ///
 /// <param name="extractExistingFile">
 /// The action to take if extraction would overwrite an existing file.
 /// </param>
 /// <seealso cref="ExtractSelectedEntries(String,ExtractExistingFileAction)"/>
 public void ExtractAll(string path, ExtractExistingFileAction extractExistingFile)
 {
     ExtractExistingFile = extractExistingFile;
     _InternalExtractAll(path, true);
 }
 /// <summary>
 ///   Extract the entry to a file in the filesystem, using the specified
 ///   behavior when extraction would overwrite an existing file.
 /// </summary>
 ///
 /// <remarks>
 /// <para>
 ///   See the remarks on the <see cref="LastModified"/> property, for some
 ///   details about how the last modified time of the file is set after
 ///   extraction.
 /// </para>
 /// </remarks>
 ///
 /// <param name="extractExistingFile">
 ///   The action to take if extraction would overwrite an existing file.
 /// </param>
 public static void Extract(this ZipEntry zipEntry, ExtractExistingFileAction extractExistingFile)
 {
     zipEntry.ExtractExistingFile = extractExistingFile;
     zipEntry.InternalExtractToBaseDir(".", null);
 }
 /// <summary>
 /// Extract the entry to the filesystem, starting at the specified base directory, and
 /// using the specified behavior when extraction would overwrite an existing file.
 /// </summary>
 ///
 /// <remarks>
 /// <para>
 /// See the remarks on the <see cref="LastModified"/> property, for some details
 /// about how the last modified time of the created file is set.
 /// </para>
 /// </remarks>
 ///
 /// <example>
 /// <code lang="C#">
 /// String sZipPath = "Airborne.zip";
 /// String sFilePath = "Readme.txt";
 /// String sRootFolder = "Digado";
 /// using (ZipFile zip = ZipFile.Read(sZipPath))
 /// {
 ///   if (zip.EntryFileNames.Contains(sFilePath))
 ///   {
 ///     // use the string indexer on the zip file
 ///     zip[sFileName].Extract(sRootFolder,
 ///                            ExtractExistingFileAction.OverwriteSilently);
 ///   }
 /// }
 /// </code>
 ///
 /// <code lang="VB">
 /// Dim sZipPath as String = "Airborne.zip"
 /// Dim sFilePath As String = "Readme.txt"
 /// Dim sRootFolder As String = "Digado"
 /// Using zip As ZipFile = ZipFile.Read(sZipPath)
 ///   If zip.EntryFileNames.Contains(sFilePath)
 ///     ' use the string indexer on the zip file
 ///     zip(sFilePath).Extract(sRootFolder, _
 ///                            ExtractExistingFileAction.OverwriteSilently)
 ///   End If
 /// End Using
 /// </code>
 /// </example>
 ///
 /// <param name="baseDirectory">the pathname of the base directory</param>
 /// <param name="extractExistingFile">
 /// The action to take if extraction would overwrite an existing file.
 /// </param>
 public void Extract(string baseDirectory, ExtractExistingFileAction extractExistingFile)
 {
     ExtractExistingFile = extractExistingFile;
     InternalExtract(baseDirectory, null, null);
 }
 public static void Extract(string zipFile, ExtractExistingFileAction action = ExtractExistingFileAction.OverwriteSilently)
 {
     using (ZipFile zip = ZipFile.Read(zipFile))
     {
         foreach (ZipEntry e in zip)
         {
             zip.ExtractAll(e.FileName, action);
         }
     }
 }
 /// <summary>
 /// Extract the entry to the filesystem, starting at the specified base directory, and
 /// using the specified behavior when extraction would overwrite an existing file.
 /// </summary>
 ///
 /// <remarks>
 /// See the remarks on the <see cref="LastModified"/> property, for some details
 /// about how the last modified time of the created file is set.
 /// </remarks>
 ///
 /// <param name="baseDirectory">the pathname of the base directory</param>
 ///
 /// <param name="extractExistingFile">The action to take if extraction would
 /// overwrite an existing file.</param>
 ///
 /// <param name="password">The Password to use for decrypting the entry.</param>
 public void ExtractWithPassword(string baseDirectory, ExtractExistingFileAction extractExistingFile, string password)
 {
     ExtractExistingFile = extractExistingFile;
     InternalExtract(baseDirectory, null, password);
 }
Beispiel #25
0
 /// <summary>
 /// Selects and Extracts a set of Entries from the ZipFile.
 /// </summary>
 ///
 /// <remarks>
 /// <para>
 /// The entries are extracted into the current working directory. When extraction would would
 /// overwrite an existing filesystem file, the action taken is as specified in the
 /// <paramref name="extractExistingFile"/> parameter.
 /// </para>
 ///
 /// <para>
 /// For information on the syntax of the string describing the entry selection criteria,
 /// see <see cref="AddSelectedFiles(String)" />.
 /// </para>
 /// </remarks>
 ///
 /// <example>
 /// This example shows how extract all XML files modified after 15 January 2009,
 /// overwriting any existing files.
 /// <code>
 /// using (ZipFile zip = ZipFile.Read(zipArchiveName))
 /// {
 ///   zip.ExtractSelectedEntries("name = *.xml  and  mtime &gt; 2009-01-15",
 ///                              ExtractExistingFileAction.OverwriteSilently);
 /// }
 /// </code>
 /// </example>
 ///
 /// <param name="selectionCriteria">the selection criteria for entries to extract.</param>
 ///
 /// <param name="extractExistingFile">
 /// The action to take if extraction would overwrite an existing file.
 /// </param>
 internal void ExtractSelectedEntries(String selectionCriteria, ExtractExistingFileAction extractExistingFile)
 {
     foreach (ZipEntry e in SelectEntries(selectionCriteria))
     {
         e.Password = _Password; // possibly null
         e.Extract(extractExistingFile);
     }
 }
Beispiel #26
0
 public void Extract(string baseDirectory, ExtractExistingFileAction extractExistingFileAction)
 {
 }
 /// <summary>
 /// Extracts all of the items in the zip archive, to the specified path in the
 /// filesystem, using the specified behavior when extraction would overwrite an
 /// existing file.
 /// </summary>
 ///
 /// <remarks>
 ///
 /// <para>
 /// This method will extract all entries in the <c>ZipFile</c> to the specified
 /// path.  For an extraction that would overwrite an existing file, the behavior
 /// is dictated by <paramref name="extractExistingFile"/>, which overrides any
 /// setting you may have made on individual ZipEntry instances.
 /// </para>
 ///
 /// <para>
 /// The action to take when an extract would overwrite an existing file
 /// applies to all entries.  If you want to set this on a per-entry basis,
 /// then you must use <see cref="ZipEntry.Extract(String,
 /// ExtractExistingFileAction)" /> or one of the similar methods.
 /// </para>
 ///
 /// <para>
 /// Calling this method is equivalent to setting the <see
 /// cref="ExtractExistingFile"/> property and then calling <see
 /// cref="ExtractAll(String)"/>.
 /// </para>
 ///
 /// <para>
 /// This method will send verbose output messages to the
 /// <see cref="StatusMessageTextWriter"/>, if it is set on the <c>ZipFile</c> instance.
 /// </para>
 /// </remarks>
 ///
 /// <example>
 /// This example extracts all the entries in a zip archive file, to the
 /// specified target directory.  It does not overwrite any existing files.
 /// <code>
 /// String TargetDirectory= "c:\\unpack";
 /// using(ZipFile zip= ZipFile.Read(ZipFileToExtract))
 /// {
 ///   zip.ExtractAll(TargetDirectory, ExtractExistingFileAction.DontOverwrite);
 /// }
 /// </code>
 ///
 /// <code lang="VB">
 /// Dim TargetDirectory As String = "c:\unpack"
 /// Using zip As ZipFile = ZipFile.Read(ZipFileToExtract)
 ///     zip.ExtractAll(TargetDirectory, ExtractExistingFileAction.DontOverwrite)
 /// End Using
 /// </code>
 /// </example>
 ///
 /// <param name="path">
 /// The path to which the contents of the zipfile will be extracted.
 /// The path can be relative or fully-qualified.
 /// </param>
 ///
 /// <param name="extractExistingFile">
 /// The action to take if extraction would overwrite an existing file.
 /// </param>
 /// <seealso cref="ExtractSelectedEntries(String,ExtractExistingFileAction)"/>
 public void ExtractAll(string path, ExtractExistingFileAction extractExistingFile)
 {
     ExtractExistingFile = extractExistingFile;
     _InternalExtractAll(path, true);
 }
Beispiel #28
0
 public void Extract(string baseDirectory, ExtractExistingFileAction extractExistingFileAction)
 {
     _zipEntry.Extract(baseDirectory, extractExistingFileAction);
 }
 /// <summary>
 /// Extracts all of the items in the zip archive, to the specified path in the
 /// filesystem, using the specified behavior when extraction would overwrite an
 /// existing file.
 /// </summary>
 ///
 /// <remarks>
 ///
 /// <para>
 /// This method will extract all entries in the <c>ZipFile</c> to the specified
 /// path.  For an extraction that would overwrite an existing file, the behavior
 /// is dictated by <paramref name="extractExistingFile"/>, which overrides any
 /// setting you may have made on individual ZipEntry instances.
 /// </para>
 ///
 /// <para>
 /// The action to take when an extract would overwrite an existing file
 /// applies to all entries.  If you want to set this on a per-entry basis,
 /// then you must use <see cref="ZipEntry.Extract(String,
 /// ExtractExistingFileAction)" /> or one of the similar methods.
 /// </para>
 ///
 /// <para>
 /// Calling this method is equivalent to setting the <see
 /// cref="ExtractExistingFile"/> property and then calling <see
 /// cref="ExtractAll(String)"/>.
 /// </para>
 ///
 /// <para>
 /// This method will send verbose output messages to the
 /// <see cref="StatusMessageTextWriter"/>, if it is set on the <c>ZipFile</c> instance.
 /// </para>
 /// </remarks>
 ///
 /// <example>
 /// This example extracts all the entries in a zip archive file, to the
 /// specified target directory.  It does not overwrite any existing files.
 /// <code>
 /// String TargetDirectory= "c:\\unpack";
 /// using(ZipFile zip= ZipFile.Read(ZipFileToExtract))
 /// {
 ///   zip.ExtractAll(TargetDirectory, ExtractExistingFileAction.DontOverwrite);
 /// }
 /// </code>
 ///
 /// <code lang="VB">
 /// Dim TargetDirectory As String = "c:\unpack"
 /// Using zip As ZipFile = ZipFile.Read(ZipFileToExtract)
 ///     zip.ExtractAll(TargetDirectory, ExtractExistingFileAction.DontOverwrite)
 /// End Using
 /// </code>
 /// </example>
 ///
 /// <param name="path">
 /// The path to which the contents of the zipfile will be extracted.
 /// The path can be relative or fully-qualified.
 /// </param>
 ///
 /// <param name="extractExistingFile">
 /// The action to take if extraction would overwrite an existing file.
 /// </param>
 /// <seealso cref="ExtractSelectedEntries(String,ExtractExistingFileAction)"/>
 public static void ExtractAll(this ZipFile zipFile, string path, ExtractExistingFileAction extractExistingFile)
 {
     zipFile.ExtractExistingFile = extractExistingFile;
     zipFile._InternalExtractAll(path, true);
 }
 public static void Extract(byte[] bytes, string path, ExtractExistingFileAction action = ExtractExistingFileAction.OverwriteSilently)
 {
     using (var memoryStream = new MemoryStream(bytes))
     {
         using (ZipFile zip = ZipFile.Read(memoryStream))
         {
             zip.ExtractAll(path, action);
         }
     }
 }
        public static void Main(String[] args)
        {
            int                       startArgs = 0;
            int                       i;
            int                       codePage                = 0;
            string                    zipfile                 = null;
            string                    targdir                 = null;
            string                    password                = null;
            List <string>             entriesToExtract        = new List <String>();
            bool                      extractToConsole        = false;
            ActionDesired             action                  = ActionDesired.Extract;
            ExtractExistingFileAction behaviorForExistingFile = ExtractExistingFileAction.DoNotOverwrite;
            bool                      wantQuiet               = false;
            bool                      wantFlatten             = false;

            System.IO.Stream bitbucket = System.IO.Stream.Null;
            System.IO.Stream outstream = null;

            // because the comments and filenames on zip entries may be UTF-8
            //System.Console.OutputEncoding = new System.Text.UTF8Encoding();

            if (args.Length == 0)
            {
                Usage();
            }
            if (args[0] == "-")
            {
                extractToConsole = true;
                outstream        = Console.OpenStandardOutput();
                startArgs        = 1;
            }

            for (i = startArgs; i < args.Length; i++)
            {
                switch (args[i])
                {
                case "-cp":
                    i++;
                    if (args.Length <= i)
                    {
                        Usage();
                    }
                    if (codePage != 0)
                    {
                        Usage();
                    }
                    System.Int32.TryParse(args[i], out codePage);
                    break;

                case "-d":
                    i++;
                    if (args.Length <= i)
                    {
                        Usage();
                    }
                    if (targdir != null)
                    {
                        Usage();
                    }
                    if (extractToConsole)
                    {
                        Usage();
                    }
                    if (action != ActionDesired.Extract)
                    {
                        Usage();
                    }
                    targdir = args[i];
                    break;

                case "-f":
                    wantFlatten = true;
                    if (action != ActionDesired.Extract)
                    {
                        Usage();
                    }
                    break;

                case "-i":
                    if (password != null)
                    {
                        Usage();
                    }
                    if (targdir != null)
                    {
                        Usage();
                    }
                    if (wantQuiet)
                    {
                        Usage();
                    }
                    if (entriesToExtract.Count > 0)
                    {
                        Usage();
                    }
                    action = ActionDesired.Info;
                    break;

                case "-l":
                    if (password != null)
                    {
                        Usage();
                    }
                    if (targdir != null)
                    {
                        Usage();
                    }
                    if (wantQuiet)
                    {
                        Usage();
                    }
                    if (entriesToExtract.Count > 0)
                    {
                        Usage();
                    }
                    if (behaviorForExistingFile == ExtractExistingFileAction.OverwriteSilently)
                    {
                        Usage();
                    }
                    action = ActionDesired.List;
                    break;

                case "-o":
                    behaviorForExistingFile = ExtractExistingFileAction.OverwriteSilently;
                    if (action != ActionDesired.Extract)
                    {
                        Usage();
                    }
                    break;

                case "-r":
                    if (wantFlatten == true)
                    {
                        Usage();
                    }
                    if (targdir != null)
                    {
                        Usage();
                    }
                    if (action == ActionDesired.Test)
                    {
                        Usage();
                    }
                    action = ActionDesired.Repair;
                    break;

                case "-p":
                    i++;
                    if (args.Length <= i)
                    {
                        Usage();
                    }
                    if (password != null)
                    {
                        Usage();
                    }
                    password = args[i];
                    break;

                case "-q":
                    if (action == ActionDesired.List)
                    {
                        Usage();
                    }
                    wantQuiet = true;
                    break;

                case "-t":
                    action = ActionDesired.Test;
                    if (targdir != null)
                    {
                        Usage();
                    }
                    //if (wantQuiet) Usage();
                    if (entriesToExtract.Count > 0)
                    {
                        Usage();
                    }
                    break;

                case "-?":
                    Usage();
                    break;

                default:
                    // positional args
                    if (zipfile == null)
                    {
                        zipfile = args[i];
                    }
                    else if (action != ActionDesired.Extract)
                    {
                        Usage();
                    }
                    else
                    {
                        entriesToExtract.Add(args[i]);
                    }
                    break;
                }
            }
            if (zipfile == null)
            {
                Console.WriteLine("unzip: No zipfile specified.\n");
                Usage();
            }

            if (!System.IO.File.Exists(zipfile))
            {
                Console.WriteLine("unzip: That zip file does not exist!\n");
                Usage();
            }

            if (targdir == null)
            {
                targdir = ".";
            }

            try
            {
                if (action == ActionDesired.Repair)
                {
                    FileSystemZip.FixZipDirectory(zipfile);
                }
                else
                {
                    var options = new ReadOptions {
                        Encoding = (codePage != 0)
                                ? System.Text.Encoding.GetEncoding(codePage)
                                : null
                    };
                    using (ZipFile zip = FileSystemZip.Read(zipfile, options))
                    {
                        if (entriesToExtract.Count > 0)
                        {
                            // extract specified entries
                            foreach (var entryToExtract in entriesToExtract)
                            {
                                // find the entry
                                ZipEntry e = zip[entryToExtract];
                                if (e == null)
                                {
                                    System.Console.WriteLine("  entry ({0}) does not exist in the zip archive.", entryToExtract);
                                }
                                else
                                {
                                    if (wantFlatten)
                                    {
                                        e.FileName = System.IO.Path.GetFileName(e.FileName);
                                    }

                                    if (password == null)
                                    {
                                        if (e.UsesEncryption)
                                        {
                                            System.Console.WriteLine("  That entry ({0}) requires a password to extract.", entryToExtract);
                                        }
                                        else if (extractToConsole)
                                        {
                                            e.Extract(outstream);
                                        }
                                        else
                                        {
                                            e.Extract(targdir, behaviorForExistingFile);
                                        }
                                    }
                                    else
                                    {
                                        if (extractToConsole)
                                        {
                                            e.ExtractWithPassword(outstream, password);
                                        }
                                        else
                                        {
                                            e.ExtractWithPassword(targdir, behaviorForExistingFile, password);
                                        }
                                    }
                                }
                            }
                        }
                        else if (action == ActionDesired.Info)
                        {
                            System.Console.WriteLine("{0}", zip.Info);
                        }
                        else
                        {
                            // extract all, or list, or test

                            // The logic here does almost the same thing as the ExtractAll() method
                            // on the ZipFile class.  But in this case we *could* have control over
                            // it, for example only extract files of a certain type, or whose names
                            // matched a certain pattern, or whose lastmodified times fit a certain
                            // condition, or use a different password for each entry, etc.  We can
                            // also display status for each entry, as here.

                            Int64 totalUncompressedSize = 0;
                            bool  header = true;
                            foreach (ZipEntry e in zip.EntriesSorted)
                            {
                                if (!wantQuiet)
                                {
                                    if (header)
                                    {
                                        System.Console.WriteLine("Zipfile: {0}", zip.Name);
                                        if ((zip.Comment != null) && (zip.Comment != ""))
                                        {
                                            System.Console.WriteLine("Comment: {0}", zip.Comment);
                                        }

                                        System.Console.WriteLine("\n{1,-22} {2,10}  {3,5}   {4,10}  {5,3} {6,8} {0}",
                                                                 "Filename", "Modified", "Size", "Ratio", "Packed", "pw?", "CRC");
                                        System.Console.WriteLine(new System.String('-', 80));
                                        header = false;
                                    }
                                    totalUncompressedSize += e.UncompressedSize;
                                    System.Console.WriteLine("{1,-22} {2,10} {3,5:F0}%   {4,10}  {5,3} {6:X8} {0}",
                                                             e.FileName,
                                                             e.LastModified.ToString("yyyy-MM-dd HH:mm:ss"),
                                                             e.UncompressedSize,
                                                             e.CompressionRatio,
                                                             e.CompressedSize,
                                                             (e.UsesEncryption) ? "Y" : "N",
                                                             e.Crc);

                                    if ((e.Comment != null) && (e.Comment != ""))
                                    {
                                        System.Console.WriteLine("  Comment: {0}", e.Comment);
                                    }
                                }

                                if (action == ActionDesired.Extract)
                                {
                                    if (e.UsesEncryption)
                                    {
                                        if (password == null)
                                        {
                                            System.Console.WriteLine("unzip: {0}: Cannot extract this entry without a password.", e.FileName);
                                        }
                                        else
                                        {
                                            if (wantFlatten)
                                            {
                                                e.FileName = System.IO.Path.GetFileName(e.FileName);
                                            }
                                            if (extractToConsole)
                                            {
                                                e.ExtractWithPassword(outstream, password);
                                            }
                                            else
                                            {
                                                e.ExtractWithPassword(targdir, behaviorForExistingFile, password);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (wantFlatten)
                                        {
                                            e.FileName = System.IO.Path.GetFileName(e.FileName);
                                        }
                                        if (extractToConsole)
                                        {
                                            e.Extract(outstream);
                                        }
                                        else
                                        {
                                            e.Extract(targdir, behaviorForExistingFile);
                                        }
                                    }
                                }
                                else if (action == ActionDesired.Test)
                                {
                                    e.ExtractWithPassword(bitbucket, password);
                                }
                            } // foreach

                            if (!wantQuiet)
                            {
                                System.Console.WriteLine(new System.String('-', 80));
                                System.Console.WriteLine("{1,-22} {2,10}  {3,5}   {4,10}  {5,3} {6,8} {0}",
                                                         zip.Entries.Count.ToString() + " files", "", totalUncompressedSize, "", "", "", "");
                            }
                        } // else (extract all)
                    }     // end using(), the underlying file is closed.
                }
            }
            catch (System.Exception ex1)
            {
                System.Console.Error.WriteLine("exception: " + ex1);
            }

            Console.WriteLine();
        }
Beispiel #32
0
 /// <summary>
 /// Selects and Extracts a set of Entries from the ZipFile.
 /// </summary>
 ///
 /// <remarks>
 /// <para>
 /// The entries are extracted into the specified directory. When extraction would would
 /// overwrite an existing filesystem file, the action taken is as specified in the
 /// <paramref name="extractExistingFile"/> parameter.
 /// </para>
 ///
 /// <para>
 /// For information on the syntax of the string describing the entry selection criteria,
 /// see <see cref="AddSelectedFiles(String)" />.
 /// </para>
 /// </remarks>
 ///
 /// <example>
 /// This example shows how extract all files  with an XML extension or with  a size larger than 100,000 bytes,
 /// and puts them in the unpack directory.  For any files that already exist in
 /// that destination directory, they will not be overwritten.
 /// <code>
 /// using (ZipFile zip = ZipFile.Read(zipArchiveName))
 /// {
 ///   zip.ExtractSelectedEntries("name = *.xml  or  size &gt; 100000",
 ///                              null,
 ///                              "unpack",
 ///                              ExtractExistingFileAction.DontOverwrite);
 /// }
 /// </code>
 /// </example>
 ///
 /// <param name="selectionCriteria">the selection criteria for entries to extract.</param>
 ///
 /// <param name="extractDirectory">
 /// The directory on the disk into which to extract. It will be created if it does not exist.
 /// </param>
 ///
 /// <param name="directoryPathInArchive">
 /// The directory in the archive from which to select entries. If null, then
 /// all directories in the archive are used.
 /// </param>
 ///
 /// <param name="extractExistingFile">
 /// The action to take if extraction would overwrite an existing file.
 /// </param>
 ///
 internal void ExtractSelectedEntries(String selectionCriteria, string directoryPathInArchive, string extractDirectory, ExtractExistingFileAction extractExistingFile)
 {
     foreach (ZipEntry e in SelectEntries(selectionCriteria, directoryPathInArchive))
     {
         e.Password = _Password; // possibly null
         e.Extract(extractDirectory, extractExistingFile);
     }
 }
 public UnzipOptions ExistingFilesAction(ExtractExistingFileAction action)
 {
     _explicitExistingFileAction = action;
      return this;
 }
 /// <summary>
 ///   Extract the entry to the filesystem, starting at the specified base
 ///   directory, and using the specified behavior when extraction would
 ///   overwrite an existing file.
 /// </summary>
 ///
 /// <remarks>
 /// <para>
 ///   See the remarks on the <see cref="LastModified"/> property, for some
 ///   details about how the last modified time of the created file is set.
 /// </para>
 /// </remarks>
 ///
 /// <example>
 /// <code lang="C#">
 /// String sZipPath = "Airborne.zip";
 /// String sFilePath = "Readme.txt";
 /// String sRootFolder = "Digado";
 /// using (ZipFile zip = ZipFile.Read(sZipPath))
 /// {
 ///   if (zip.EntryFileNames.Contains(sFilePath))
 ///   {
 ///     // use the string indexer on the zip file
 ///     zip[sFileName].Extract(sRootFolder,
 ///                            ExtractExistingFileAction.OverwriteSilently);
 ///   }
 /// }
 /// </code>
 ///
 /// <code lang="VB">
 /// Dim sZipPath as String = "Airborne.zip"
 /// Dim sFilePath As String = "Readme.txt"
 /// Dim sRootFolder As String = "Digado"
 /// Using zip As ZipFile = ZipFile.Read(sZipPath)
 ///   If zip.EntryFileNames.Contains(sFilePath)
 ///     ' use the string indexer on the zip file
 ///     zip(sFilePath).Extract(sRootFolder, _
 ///                            ExtractExistingFileAction.OverwriteSilently)
 ///   End If
 /// End Using
 /// </code>
 /// </example>
 ///
 /// <param name="baseDirectory">the pathname of the base directory</param>
 /// <param name="extractExistingFile">
 /// The action to take if extraction would overwrite an existing file.
 /// </param>
 public void Extract(string baseDirectory, ExtractExistingFileAction extractExistingFile)
 {
     ExtractExistingFile = extractExistingFile;
     InternalExtractToBaseDir(baseDirectory, null, _container, _Source, FileName);
 }
 /// <summary>
 /// Selects and Extracts a set of Entries from the ZipFile.
 /// </summary>
 ///
 /// <remarks>
 /// <para>
 /// The entries are extracted into the current working directory. When extraction would would
 /// overwrite an existing filesystem file, the action taken is as specified in the
 /// <paramref name="extractExistingFile"/> parameter.
 /// </para>
 ///
 /// <para>
 /// For information on the syntax of the string describing the entry selection criteria,
 /// see <see cref="AddSelectedFiles(String)" />.
 /// </para>
 /// </remarks>
 ///
 /// <example>
 /// This example shows how extract all XML files modified after 15 January 2009,
 /// overwriting any existing files.
 /// <code>
 /// using (ZipFile zip = ZipFile.Read(zipArchiveName))
 /// {
 ///   zip.ExtractSelectedEntries("name = *.xml  and  mtime &gt; 2009-01-15",
 ///                              ExtractExistingFileAction.OverwriteSilently);
 /// }
 /// </code>
 /// </example>
 ///
 /// <param name="selectionCriteria">the selection criteria for entries to extract.</param>
 ///
 /// <param name="extractExistingFile">
 /// The action to take if extraction would overwrite an existing file.
 /// </param>
 public void ExtractSelectedEntries(String selectionCriteria, ExtractExistingFileAction extractExistingFile)
 {
     foreach (ZipEntry e in SelectEntries(selectionCriteria))
     {
         e.Extract(extractExistingFile);
     }
 }
 /// <summary>
 ///   Extract the entry to a file in the filesystem, using the specified
 ///   behavior when extraction would overwrite an existing file.
 /// </summary>
 ///
 /// <remarks>
 /// <para>
 ///   See the remarks on the <see cref="LastModified"/> property, for some
 ///   details about how the last modified time of the file is set after
 ///   extraction.
 /// </para>
 /// </remarks>
 ///
 /// <param name="extractExistingFile">
 ///   The action to take if extraction would overwrite an existing file.
 /// </param>
 public void Extract(ExtractExistingFileAction extractExistingFile)
 {
     ExtractExistingFile = extractExistingFile;
     InternalExtractToBaseDir(".", null, _container, _Source, FileName);
 }
 /// <summary>
 /// Extracts all of the items in the zip archive, to the specified path in the
 /// filesystem, using the specified behavior when extraction would overwrite an
 /// existing file.
 /// </summary>
 ///
 /// <remarks>
 ///
 /// <para>
 /// This method will extract all entries in the <c>ZipFile</c> to the specified
 /// path.  For an extraction that would overwrite an existing file, the behavior
 /// is dictated by <paramref name="extractExistingFile"/>, which overrides any
 /// setting you may have made on individual ZipEntry instances.
 /// </para>
 ///
 /// <para>
 /// The action to take when an extract would overwrite an existing file
 /// applies to all entries.  If you want to set this on a per-entry basis,
 /// then you must use <see cref="ZipEntry.Extract(String,
 /// ExtractExistingFileAction)" /> or one of the similar methods.
 /// </para>
 ///
 /// <para>
 /// Calling this method is equivalent to setting the <see
 /// cref="ExtractExistingFile"/> property and then calling <see
 /// cref="ExtractAll(String)"/>.
 /// </para>
 ///
 /// <para>
 /// This method will send verbose output messages to the
 /// <see cref="StatusMessageTextWriter"/>, if it is set on the <c>ZipFile</c> instance.
 /// </para>
 /// </remarks>
 ///
 /// <example>
 /// This example extracts all the entries in a zip archive file, to the
 /// specified target directory.  It does not overwrite any existing files.
 /// <code>
 /// String TargetDirectory= "c:\\unpack";
 /// using(ZipFile zip= ZipFile.Read(ZipFileToExtract))
 /// {
 ///   zip.ExtractAll(TargetDirectory, ExtractExistingFileAction.DontOverwrite);
 /// }
 /// </code>
 ///
 /// <code lang="VB">
 /// Dim TargetDirectory As String = "c:\unpack"
 /// Using zip As ZipFile = ZipFile.Read(ZipFileToExtract)
 ///     zip.ExtractAll(TargetDirectory, ExtractExistingFileAction.DontOverwrite)
 /// End Using
 /// </code>
 /// </example>
 ///
 /// <param name="path">
 /// The path to which the contents of the zipfile will be extracted.
 /// The path can be relative or fully-qualified.
 /// </param>
 ///
 /// <param name="extractExistingFile">
 /// The action to take if extraction would overwrite an existing file.
 /// </param>
 /// <seealso cref="ExtractSelectedEntries(String,ExtractExistingFileAction)"/>
 public static void ExtractAll(this ZipFile zipFile, string path, ExtractExistingFileAction extractExistingFile)
 {
     zipFile.ExtractExistingFile = extractExistingFile;
     zipFile._InternalExtractAll(path, true);
 }