Merge pull request #3715 from kumpera/fix-44707
[mono.git] / mcs / class / WindowsBase / ZipSharp / UnzipArchive.cs
1 // UnzipArchive.cs
2 //
3 // Copyright (c) 2008 [copyright holders]
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 //
23 //
24
25 using System;
26 using System.IO;
27
28 namespace zipsharp
29 {
30         class UnzipArchive : IDisposable
31         {
32                 string[] files;
33                 
34                 internal bool FileActive {
35                         get; set;
36                 }
37                 
38                 string[] Files {
39                         get {
40                                 if (files == null)
41                                         files = NativeVersion.Use32Bit ? NativeUnzip.GetFiles32 (Handle) : NativeUnzip.GetFiles64 (Handle);
42                                 return files;
43                         }
44                 }
45                 
46                 internal UnzipHandle Handle {
47                         get; private set;
48                 }
49                 
50                 ZipStream Stream {
51                         get; set;
52                 }
53
54                 public UnzipArchive (string filename)
55                         : this (File.Open (filename, FileMode.Open), true)
56                 {
57                         
58                 }
59
60                 public UnzipArchive (Stream stream)
61                         : this (stream, false)
62                 {
63                         
64                 }
65
66                 public UnzipArchive (Stream stream, bool ownsStream)
67                 {
68                         Stream = new ZipStream (stream, ownsStream);
69                         Handle = NativeVersion.Use32Bit ? NativeUnzip.OpenArchive32 (Stream.IOFunctions32) : NativeUnzip.OpenArchive64 (Stream.IOFunctions64);
70                 }
71
72                 public void Dispose ()
73                 {
74                         NativeUnzip.CloseArchive (Handle);
75                         Stream.Close ();
76                 }
77
78                 public System.IO.Packaging.CompressionOption GetCompressionLevel (string file)
79                 {
80                         using (UnzipReadStream stream = (UnzipReadStream) GetStream (file))
81                                 return stream.CompressionLevel;
82                 }
83
84                 public string[] GetFiles ()
85                 {
86                         return (string []) Files.Clone ();
87                 }
88
89                 public Stream GetStream (string name)
90                 {
91                         foreach (string file in Files)
92                         {
93                                 if (name.Equals(file, StringComparison.OrdinalIgnoreCase))
94                                 {
95                                         System.IO.Packaging.CompressionOption option;
96                                         NativeUnzip.OpenFile (Handle, name, out option);
97                                         return new UnzipReadStream (this, option);
98                                 }
99                         }
100                         
101                         throw new Exception ("The file doesn't exist in the zip archive");
102                 }
103         }
104 }