2 // ZipFileExtensions.cs
5 // João Matos <joao.matos@xamarin.com>
6 // Martin Baulig <martin.baulig@xamarin.com>
8 // Copyright (c) 2013 Xamarin Inc. (http://www.xamarin.com)
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 namespace System.IO.Compression
31 public static class ZipFileExtensions
33 public static ZipArchiveEntry CreateEntryFromFile (
34 this ZipArchive destination, string sourceFileName,
37 return CreateEntryFromFile (destination, sourceFileName, entryName,
38 CompressionLevel.Fastest);
41 public static ZipArchiveEntry CreateEntryFromFile (
42 this ZipArchive destination, string sourceFileName,
43 string entryName, CompressionLevel compressionLevel)
45 if (destination == null)
46 throw new ArgumentNullException ("destination");
48 if (sourceFileName == null)
49 throw new ArgumentNullException ("sourceFileName");
51 if (entryName == null)
52 throw new ArgumentNullException ("entryName");
54 ZipArchiveEntry entry;
55 using (Stream stream = File.Open (sourceFileName, FileMode.Open,
56 FileAccess.Read, FileShare.Read))
58 var zipArchiveEntry = destination.CreateEntry (entryName, compressionLevel);
59 zipArchiveEntry.LastWriteTime = File.GetLastWriteTimeUtc(sourceFileName);
61 using (Stream entryStream = zipArchiveEntry.Open ())
62 stream.CopyTo (entryStream);
64 entry = zipArchiveEntry;
70 public static void ExtractToDirectory (
71 this ZipArchive source,
72 string destinationDirectoryName)
75 throw new ArgumentNullException ("source");
77 if (destinationDirectoryName == null)
78 throw new ArgumentNullException ("destinationDirectoryName");
80 var destDirInfo = Directory.CreateDirectory (destinationDirectoryName);
81 string fullName = destDirInfo.FullName;
83 foreach (var zipEntry in source.Entries)
85 var fullPath = Path.GetFullPath (Path.Combine (fullName, zipEntry.FullName));
87 var isFileName = Path.GetFileName (fullPath).Length != 0;
88 var dirPath = isFileName ? Path.GetDirectoryName (fullPath) : fullPath;
89 Directory.CreateDirectory (dirPath);
92 zipEntry.ExtractToFile (fullPath, false);
96 public static void ExtractToFile (
97 this ZipArchiveEntry source,
98 string destinationFileName)
100 ExtractToFile (source, destinationFileName, overwrite: false);
103 public static void ExtractToFile (
104 this ZipArchiveEntry source, string destinationFileName,
108 throw new ArgumentNullException ("source");
110 if (destinationFileName == null)
111 throw new ArgumentNullException ("destinationFileName");
113 var mode = overwrite ? FileMode.Create : FileMode.CreateNew;
114 using (var stream = File.Open (destinationFileName, mode, FileAccess.Write))
116 using (var stream2 = source.Open ())
117 stream2.CopyTo(stream);
120 File.SetLastWriteTime(destinationFileName, source.LastWriteTime.DateTime);