Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/Packaging/DotNetZip/ZipFile.Extract.cs @ 18191

Last change on this file since 18191 was 12074, checked in by sraggl, 10 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 12.5 KB
Line 
1// ZipFile.Extract.cs
2// ------------------------------------------------------------------
3//
4// Copyright (c) 2009 Dino Chiesa.
5// All rights reserved.
6//
7// This code module is part of DotNetZip, a zipfile class library.
8//
9// ------------------------------------------------------------------
10//
11// This code is licensed under the Microsoft Public License.
12// See the file License.txt for the license details.
13// More info on: http://dotnetzip.codeplex.com
14//
15// ------------------------------------------------------------------
16//
17// last saved (in emacs):
18// Time-stamp: <2011-July-31 14:45:18>
19//
20// ------------------------------------------------------------------
21//
22// This module defines the methods for Extract operations on zip files.
23//
24// ------------------------------------------------------------------
25//
26
27
28using System;
29using System.IO;
30using System.Collections.Generic;
31
32namespace OfficeOpenXml.Packaging.Ionic.Zip
33{
34
35    internal partial class ZipFile
36    {
37
38        /// <summary>
39        /// Extracts all of the items in the zip archive, to the specified path in the
40        /// filesystem.  The path can be relative or fully-qualified.
41        /// </summary>
42        ///
43        /// <remarks>
44        /// <para>
45        ///   This method will extract all entries in the <c>ZipFile</c> to the
46        ///   specified path.
47        /// </para>
48        ///
49        /// <para>
50        ///   If an extraction of a file from the zip archive would overwrite an
51        ///   existing file in the filesystem, the action taken is dictated by the
52        ///   ExtractExistingFile property, which overrides any setting you may have
53        ///   made on individual ZipEntry instances.  By default, if you have not
54        ///   set that property on the <c>ZipFile</c> instance, the entry will not
55        ///   be extracted, the existing file will not be overwritten and an
56        ///   exception will be thrown. To change this, set the property, or use the
57        ///   <see cref="ZipFile.ExtractAll(string,
58        ///   Ionic.Zip.ExtractExistingFileAction)" /> overload that allows you to
59        ///   specify an ExtractExistingFileAction parameter.
60        /// </para>
61        ///
62        /// <para>
63        ///   The action to take when an extract would overwrite an existing file
64        ///   applies to all entries.  If you want to set this on a per-entry basis,
65        ///   then you must use one of the <see
66        ///   cref="ZipEntry.Extract()">ZipEntry.Extract</see> methods.
67        /// </para>
68        ///
69        /// <para>
70        ///   This method will send verbose output messages to the <see
71        ///   cref="StatusMessageTextWriter"/>, if it is set on the <c>ZipFile</c>
72        ///   instance.
73        /// </para>
74        ///
75        /// <para>
76        /// You may wish to take advantage of the <c>ExtractProgress</c> event.
77        /// </para>
78        ///
79        /// <para>
80        ///   About timestamps: When extracting a file entry from a zip archive, the
81        ///   extracted file gets the last modified time of the entry as stored in
82        ///   the archive. The archive may also store extended file timestamp
83        ///   information, including last accessed and created times. If these are
84        ///   present in the <c>ZipEntry</c>, then the extracted file will also get
85        ///   these times.
86        /// </para>
87        ///
88        /// <para>
89        ///   A Directory entry is somewhat different. It will get the times as
90        ///   described for a file entry, but, if there are file entries in the zip
91        ///   archive that, when extracted, appear in the just-created directory,
92        ///   then when those file entries are extracted, the last modified and last
93        ///   accessed times of the directory will change, as a side effect.  The
94        ///   result is that after an extraction of a directory and a number of
95        ///   files within the directory, the last modified and last accessed
96        ///   timestamps on the directory will reflect the time that the last file
97        ///   was extracted into the directory, rather than the time stored in the
98        ///   zip archive for the directory.
99        /// </para>
100        ///
101        /// <para>
102        ///   To compensate, when extracting an archive with <c>ExtractAll</c>,
103        ///   DotNetZip will extract all the file and directory entries as described
104        ///   above, but it will then make a second pass on the directories, and
105        ///   reset the times on the directories to reflect what is stored in the
106        ///   zip archive.
107        /// </para>
108        ///
109        /// <para>
110        ///   This compensation is performed only within the context of an
111        ///   <c>ExtractAll</c>. If you call <c>ZipEntry.Extract</c> on a directory
112        ///   entry, the timestamps on directory in the filesystem will reflect the
113        ///   times stored in the zip.  If you then call <c>ZipEntry.Extract</c> on
114        ///   a file entry, which is extracted into the directory, the timestamps on
115        ///   the directory will be updated to the current time.
116        /// </para>
117        /// </remarks>
118        ///
119        /// <example>
120        ///   This example extracts all the entries in a zip archive file, to the
121        ///   specified target directory.  The extraction will overwrite any
122        ///   existing files silently.
123        ///
124        /// <code>
125        /// String TargetDirectory= "unpack";
126        /// using(ZipFile zip= ZipFile.Read(ZipFileToExtract))
127        /// {
128        ///     zip.ExtractExistingFile= ExtractExistingFileAction.OverwriteSilently;
129        ///     zip.ExtractAll(TargetDirectory);
130        /// }
131        /// </code>
132        ///
133        /// <code lang="VB">
134        /// Dim TargetDirectory As String = "unpack"
135        /// Using zip As ZipFile = ZipFile.Read(ZipFileToExtract)
136        ///     zip.ExtractExistingFile= ExtractExistingFileAction.OverwriteSilently
137        ///     zip.ExtractAll(TargetDirectory)
138        /// End Using
139        /// </code>
140        /// </example>
141        ///
142        /// <seealso cref="Ionic.Zip.ZipFile.ExtractProgress"/>
143        /// <seealso cref="Ionic.Zip.ZipFile.ExtractExistingFile"/>
144        ///
145        /// <param name="path">
146        ///   The path to which the contents of the zipfile will be extracted.
147        ///   The path can be relative or fully-qualified.
148        /// </param>
149        ///
150        public void ExtractAll(string path)
151        {
152            _InternalExtractAll(path, true);
153        }
154
155
156
157        /// <summary>
158        /// Extracts all of the items in the zip archive, to the specified path in the
159        /// filesystem, using the specified behavior when extraction would overwrite an
160        /// existing file.
161        /// </summary>
162        ///
163        /// <remarks>
164        ///
165        /// <para>
166        /// This method will extract all entries in the <c>ZipFile</c> to the specified
167        /// path.  For an extraction that would overwrite an existing file, the behavior
168        /// is dictated by <paramref name="extractExistingFile"/>, which overrides any
169        /// setting you may have made on individual ZipEntry instances.
170        /// </para>
171        ///
172        /// <para>
173        /// The action to take when an extract would overwrite an existing file
174        /// applies to all entries.  If you want to set this on a per-entry basis,
175        /// then you must use <see cref="ZipEntry.Extract(String,
176        /// ExtractExistingFileAction)" /> or one of the similar methods.
177        /// </para>
178        ///
179        /// <para>
180        /// Calling this method is equivalent to setting the <see
181        /// cref="ExtractExistingFile"/> property and then calling <see
182        /// cref="ExtractAll(String)"/>.
183        /// </para>
184        ///
185        /// <para>
186        /// This method will send verbose output messages to the
187        /// <see cref="StatusMessageTextWriter"/>, if it is set on the <c>ZipFile</c> instance.
188        /// </para>
189        /// </remarks>
190        ///
191        /// <example>
192        /// This example extracts all the entries in a zip archive file, to the
193        /// specified target directory.  It does not overwrite any existing files.
194        /// <code>
195        /// String TargetDirectory= "c:\\unpack";
196        /// using(ZipFile zip= ZipFile.Read(ZipFileToExtract))
197        /// {
198        ///   zip.ExtractAll(TargetDirectory, ExtractExistingFileAction.DontOverwrite);
199        /// }
200        /// </code>
201        ///
202        /// <code lang="VB">
203        /// Dim TargetDirectory As String = "c:\unpack"
204        /// Using zip As ZipFile = ZipFile.Read(ZipFileToExtract)
205        ///     zip.ExtractAll(TargetDirectory, ExtractExistingFileAction.DontOverwrite)
206        /// End Using
207        /// </code>
208        /// </example>
209        ///
210        /// <param name="path">
211        /// The path to which the contents of the zipfile will be extracted.
212        /// The path can be relative or fully-qualified.
213        /// </param>
214        ///
215        /// <param name="extractExistingFile">
216        /// The action to take if extraction would overwrite an existing file.
217        /// </param>
218        /// <seealso cref="ExtractSelectedEntries(String,ExtractExistingFileAction)"/>
219        internal void ExtractAll(string path, ExtractExistingFileAction extractExistingFile)
220        {
221            ExtractExistingFile = extractExistingFile;
222            _InternalExtractAll(path, true);
223        }
224
225
226        private void _InternalExtractAll(string path, bool overrideExtractExistingProperty)
227        {
228            bool header = Verbose;
229            _inExtractAll = true;
230            try
231            {
232                OnExtractAllStarted(path);
233
234                int n = 0;
235                foreach (ZipEntry e in _entries.Values)
236                {
237                    if (header)
238                    {
239                        StatusMessageTextWriter.WriteLine("\n{1,-22} {2,-8} {3,4}   {4,-8}  {0}",
240                                  "Name", "Modified", "Size", "Ratio", "Packed");
241                        StatusMessageTextWriter.WriteLine(new System.String('-', 72));
242                        header = false;
243                    }
244                    if (Verbose)
245                    {
246                        StatusMessageTextWriter.WriteLine("{1,-22} {2,-8} {3,4:F0}%   {4,-8} {0}",
247                                  e.FileName,
248                                  e.LastModified.ToString("yyyy-MM-dd HH:mm:ss"),
249                                  e.UncompressedSize,
250                                  e.CompressionRatio,
251                                  e.CompressedSize);
252                        if (!String.IsNullOrEmpty(e.Comment))
253                            StatusMessageTextWriter.WriteLine("  Comment: {0}", e.Comment);
254                    }
255                    e.Password = _Password;  // this may be null
256                    OnExtractEntry(n, true, e, path);
257                    if (overrideExtractExistingProperty)
258                        e.ExtractExistingFile = this.ExtractExistingFile;
259                    e.Extract(path);
260                    n++;
261                    OnExtractEntry(n, false, e, path);
262                    if (_extractOperationCanceled)
263                        break;
264                }
265
266                if (!_extractOperationCanceled)
267                {
268                    // workitem 8264:
269                    // now, set times on directory entries, again.
270                    // The problem is, extracting a file changes the times on the parent
271                    // directory.  So after all files have been extracted, we have to
272                    // run through the directories again.
273                    foreach (ZipEntry e in _entries.Values)
274                    {
275                        // check if it is a directory
276                        if ((e.IsDirectory) || (e.FileName.EndsWith("/")))
277                        {
278                            string outputFile = (e.FileName.StartsWith("/"))
279                                ? Path.Combine(path, e.FileName.Substring(1))
280                                : Path.Combine(path, e.FileName);
281
282                            e._SetTimes(outputFile, false);
283                        }
284                    }
285                    OnExtractAllCompleted(path);
286                }
287
288            }
289            finally
290            {
291
292                _inExtractAll = false;
293            }
294        }
295
296
297    }
298}
Note: See TracBrowser for help on using the repository browser.