forked from xamarin/LibZipSharp
/
ZipEntry.cs
386 lines (335 loc) · 12.8 KB
/
ZipEntry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
//
// ZipEntry.cs
//
// Author:
// Marek Habersack <grendel@twistedcode.net>
//
// Copyright (c) 2016 Xamarin, Inc (http://xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
namespace Xamarin.Tools.Zip
{
/// <summary>
/// Represents a single entry in the ZIP archive.
/// </summary>
public abstract partial class ZipEntry
{
// Must not exceed Int32.MaxValue
const int ReadBufSize = 16384;
ZipArchive archive;
Native.zip_stat_t stat;
StatFlags valid;
short localExtraFieldsCount;
short centralExtraFieldsCount;
/// <summary>
/// Archive to which this entry belongs
/// </summary>
/// <value>The archive.</value>
public ZipArchive Archive {
get { return archive; }
}
/// <summary>
/// Gets index of the entry in the ZIP directory.
/// </summary>
/// <value>Entry index</value>
public ulong Index { get; private set; }
/// <summary>
/// Gets the full entry name.
/// </summary>
/// <value>The entry name.</value>
public string FullName { get; private set; }
/// <summary>
/// Gets the uncompressed entry size.
/// </summary>
/// <value>Uncompressed entry size.</value>
public ulong Size { get; private set; }
/// <summary>
/// Gets the compressed size of the entry.
/// </summary>
/// <value>Compressed entry size.</value>
public ulong CompressedSize { get; private set; }
/// <summary>
/// Gets the last modification time of the ZIP entry. The time might be in the UTC
/// timezone if it was read from the Unix Extended Field. Check the <see cref="DateTime.Kind"/>
/// property before setting the time stamp.
/// </summary>
/// <value>Last modification time or <see cref="DateTime.MinValue"/> if invalid/unset</value>
public DateTime ModificationTime { get; internal set; } = DateTime.MinValue;
/// <summary>
/// Gets the entry CRC
/// </summary>
/// <value>Entry CRC</value>
public uint CRC { get; private set; }
/// <summary>
/// Gets the compression method.
/// </summary>
/// <value>The compression method.</value>
public CompressionMethod CompressionMethod { get; private set; }
/// <summary>
/// Gets the encryption method.
/// </summary>
/// <value>The encryption method.</value>
public EncryptionMethod EncryptionMethod { get; private set; }
/// <summary>
/// Indicates whether this entry represents a directory
/// </summary>
/// <value><c>true</c> for directory entry</value>
public bool IsDirectory { get; internal set; }
public uint ExternalAttributes { get; private set; }
public OperatingSystem OperatingSystem { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="T:Xamarin.ZipSharp.ZipEntry"/> class.
/// </summary>
/// <param name="archive">ZIP archive instance.</param>
/// <param name="stat">entry status information.</param>
internal ZipEntry (ZipArchive archive, Native.zip_stat_t stat)
{
if (archive == null)
throw new ArgumentNullException (nameof (archive));
this.archive = archive;
this.stat = stat;
}
internal void Init ()
{
valid = (StatFlags)stat.valid;
// We mustn't free stat.name, it's handled by libzip and freeing it here would cause a crash.
FullName = GetStatField (StatFlags.Name, () => Utilities.GetStringFromNativeAnsi (stat.name), String.Empty);
Index = GetStatField (StatFlags.Index, () => stat.index);
Size = GetStatField (StatFlags.Size, () => stat.size);
CompressedSize = GetStatField (StatFlags.CompSize, () => stat.comp_size);
// This value may be overriden on Unix systems if the extended fields with ID 0x000d, 0x5455 or 0x5855 are found for this entry
ModificationTime = GetStatField (StatFlags.MTime, () => Utilities.DateTimeFromUnixTime ((ulong)stat.mtime.ToInt64 ()), Utilities.UnixEpoch);
CRC = GetStatField (StatFlags.CRC, () => stat.crc);
CompressionMethod = GetStatField (StatFlags.CompMethod, () => {
if (Enum.IsDefined (typeof (CompressionMethod), stat.comp_method))
return (CompressionMethod)stat.comp_method;
return CompressionMethod.Unknown;
});
EncryptionMethod = GetStatField (StatFlags.EncryptionMethod, () => {
if (Enum.IsDefined (typeof (EncryptionMethod), stat.encryption_method))
return (EncryptionMethod)stat.encryption_method;
return EncryptionMethod.Unknown;
});
IsDirectory = Size == 0 && FullName.EndsWith ("/", StringComparison.Ordinal);
byte opsys;
uint xattr;
if (Native.zip_file_get_external_attributes (archive.ArchivePointer, Index, OperationFlags.None, out opsys, out xattr) == 0) {
ExternalAttributes = xattr;
OperatingSystem = (OperatingSystem)opsys;
} else {
OperatingSystem = OperatingSystem.DOS;
ExternalAttributes = 0;
}
localExtraFieldsCount = Native.zip_file_extra_fields_count (archive.ArchivePointer, Index, OperationFlags.Local);
centralExtraFieldsCount = Native.zip_file_extra_fields_count (archive.ArchivePointer, Index, OperationFlags.Central);
PlatformServices.Instance.ReadAndProcessExtraFields (this);
}
/// <summary>
/// Delete this entry from the associated archive. An exception is thrown if the entry's index doesn't exist in the archive.
/// </summary>
/// <exception cref="ZipException">Thrown if the entry's index is invalid for this archive</exception>
/// <seealso cref="ZipArchive.DeleteEntry"/>
public void Delete ()
{
archive.DeleteEntry (Index);
}
public void Extract (Stream stream)
{
var args = new EntryExtractEventArgs {
Entry = this,
ProcessedSoFar = 0
};
OnExtract (args);
if (!IsDirectory) {
OperationFlags flags = OperationFlags.None;
IntPtr file = IntPtr.Zero;
try {
file = Native.zip_fopen_index (archive.ArchivePointer, Index, flags);
if (file == IntPtr.Zero)
throw archive.GetErrorException ();
DoExtract (file, stream, args);
}
finally {
if (file != IntPtr.Zero)
Native.zip_fclose (file);
}
}
OnExtract (args);
}
/// <summary>
/// Extract this entry in directory specified by <paramref name="destinationDir"/>, optionally changing the entry's name to the
/// one given in <paramref name="destinationFileName"/>. The destination file is opened using mode specified in the
/// <paramref name="outputFileMode"/> parameter.
/// </summary>
/// <param name="destinationDir">Destination dir.</param>
/// <param name="destinationFileName">Destination file name.</param>
/// <param name="outputFileMode">Output file mode.</param>
public string Extract (string destinationDir = null, string destinationFileName = null, FileMode outputFileMode = FileMode.Create)
{
destinationDir = destinationDir?.Trim ();
if (String.IsNullOrEmpty (destinationDir))
destinationDir = String.IsNullOrEmpty (archive.DefaultExtractionDir) ? "." : archive.DefaultExtractionDir;
destinationFileName = destinationFileName?.Trim ();
string path = Path.Combine (destinationDir, String.IsNullOrEmpty (destinationFileName) ? FullName : destinationFileName);
string dir = Path.GetDirectoryName (path);
Directory.CreateDirectory (dir);
var args = new EntryExtractEventArgs {
Entry = this,
ProcessedSoFar = 0
};
OnExtract (args);
if (!IsDirectory) {
// TODO: handle non-regular files
OperationFlags flags = OperationFlags.None;
IntPtr file = IntPtr.Zero;
try {
file = Native.zip_fopen_index (archive.ArchivePointer, Index, flags);
if (file == IntPtr.Zero)
throw archive.GetErrorException ();
DoExtract (file, path, outputFileMode, args);
PlatformServices.Instance.SetFileProperties (this, path, true);
} finally {
if (file != IntPtr.Zero)
Native.zip_fclose (file);
}
}
OnExtract (args);
return path;
}
bool TranslateLocation (ZipHeaderLocation location, out OperationFlags flags)
{
flags = OperationFlags.None;
switch (location) {
case ZipHeaderLocation.Both:
case ZipHeaderLocation.Any:
if (localExtraFieldsCount <= 0 && centralExtraFieldsCount <= 0)
return false;
// Don't set any flags here, native api errors out if it has both central and local flags set
break;
case ZipHeaderLocation.Central:
if (centralExtraFieldsCount <= 0)
return false;
flags = OperationFlags.Central;
break;
case ZipHeaderLocation.Local:
if (localExtraFieldsCount <= 0)
return false;
flags = OperationFlags.Local;
break;
}
return true;
}
ushort GetFieldCount (ushort fieldID, ZipHeaderLocation location, OperationFlags flags)
{
short count = Native.zip_file_extra_fields_count_by_id (archive.ArchivePointer, Index, fieldID, flags);
if (count < 0)
count = 0;
return (ushort)count;
}
public bool ExtraFieldPresent (ushort fieldID, ZipHeaderLocation location = ZipHeaderLocation.Any)
{
OperationFlags flags = OperationFlags.None;
if (!TranslateLocation (location, out flags))
return false;
if (flags != OperationFlags.Central && GetFieldCount (fieldID, location, OperationFlags.Local) > 0)
return true;
return GetFieldCount (fieldID, location, OperationFlags.Central) > 0;
}
public IList <ExtraField> GetExtraField (ushort fieldID, ZipHeaderLocation location = ZipHeaderLocation.Any, bool required = false)
{
OperationFlags flags = OperationFlags.None;
if (!TranslateLocation (location, out flags))
return null;
var ret = new List<ExtraField> ();
if (flags == OperationFlags.None) {
GatherExtraFields (GetFieldCount (fieldID, location, OperationFlags.Local), fieldID, OperationFlags.Local, ret, required);
if (location == ZipHeaderLocation.Any && ret.Count > 0)
return ret;
GatherExtraFields (GetFieldCount (fieldID, location, OperationFlags.Central), fieldID, OperationFlags.Central, ret, required);
} else
GatherExtraFields (GetFieldCount (fieldID, location, flags), fieldID, flags, ret, required);
return ret.Count > 0 ? ret : null;
}
void GatherExtraFields (ushort count, ushort fieldID, OperationFlags flags, List<ExtraField> fields, bool required)
{
if (count == 0)
return;
ushort fieldLength;
IntPtr fieldData;
for (ushort i = 0; i < count; i++) {
fieldData = Native.zip_file_extra_field_get_by_id (archive.ArchivePointer, Index, fieldID, (ushort)i, out fieldLength, flags);
if (fieldData == IntPtr.Zero) {
if (!required)
continue;
throw archive.GetErrorException ();
}
byte [] data;
if (fieldLength > 0) {
data = new byte [fieldLength];
Marshal.Copy (fieldData, data, 0, fieldLength);
} else
data = null;
fields.Add (new ExtraField {
RawData = data,
EntryIndex = Index,
FieldIndex = i,
ID = fieldID,
Local = true,
Length = fieldLength
});
}
}
void DoExtract (IntPtr zipFile, string destinationPath, FileMode outputFileMode, EntryExtractEventArgs args)
{
using (FileStream fs = File.Open (destinationPath, outputFileMode)) {
DoExtract (zipFile, fs, args);
}
}
void DoExtract (IntPtr zipFile, Stream destinationStream, EntryExtractEventArgs args)
{
var buf = new byte [ReadBufSize];
long nread;
while ((nread = Native.zip_fread (zipFile, buf, (ulong)buf.Length)) > 0) {
destinationStream.Write (buf, 0, (int)nread);
args.ProcessedSoFar += (ulong)nread;
OnExtract (args);
}
}
void OnExtract (EntryExtractEventArgs args = null)
{
if (args == null) {
args = new EntryExtractEventArgs {
Entry = this,
ProcessedSoFar = 100 // One-shot, assuming completion
};
}
archive.OnEntryExtract (args);
}
T GetStatField <T> (StatFlags field, Func<T> value, T deflt = default (T))
{
if (valid.HasFlag (field))
return value ();
return deflt;
}
}
}