Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ZipArchive.cs
Go to the documentation of this file.
3using System.Text;
4
6
7public class ZipArchive : IDisposable
8{
9 private readonly Stream _archiveStream;
10
12
14
16
18
20
22
23 private bool _readEntries;
24
25 private bool _leaveOpen;
26
28
29 private bool _isDisposed;
30
31 private uint _numberOfThisDisk;
32
34
36
37 private byte[] _archiveComment;
38
40
54
55 public ZipArchiveMode Mode => _mode;
56
58
60
62
64 {
65 get
66 {
67 return _entryNameEncoding;
68 }
69 private set
70 {
71 if (value != null && (value.Equals(Encoding.BigEndianUnicode) || value.Equals(Encoding.Unicode)))
72 {
73 throw new ArgumentException(System.SR.EntryNameEncodingNotSupported, "EntryNameEncoding");
74 }
76 }
77 }
78
83
85 : this(stream, mode, leaveOpen: false, null)
86 {
87 }
88
90 : this(stream, mode, leaveOpen, null)
91 {
92 }
93
95 {
96 if (stream == null)
97 {
98 throw new ArgumentNullException("stream");
99 }
101 Stream stream2 = null;
102 try
103 {
104 _backingStream = null;
105 switch (mode)
106 {
107 case ZipArchiveMode.Create:
108 if (!stream.CanWrite)
109 {
111 }
112 break;
113 case ZipArchiveMode.Read:
114 if (!stream.CanRead)
115 {
117 }
118 if (!stream.CanSeek)
119 {
121 stream2 = (stream = new MemoryStream());
123 stream.Seek(0L, SeekOrigin.Begin);
124 }
125 break;
126 case ZipArchiveMode.Update:
127 if (!stream.CanRead || !stream.CanWrite || !stream.CanSeek)
128 {
130 }
131 break;
132 default:
133 throw new ArgumentOutOfRangeException("mode");
134 }
135 _mode = mode;
136 if (mode == ZipArchiveMode.Create && !stream.CanSeek)
137 {
139 }
140 else
141 {
143 }
144 _archiveStreamOwner = null;
145 if (mode == ZipArchiveMode.Create)
146 {
147 _archiveReader = null;
148 }
149 else
150 {
152 }
156 _readEntries = false;
159 _isDisposed = false;
161 _archiveComment = null;
162 switch (mode)
163 {
164 case ZipArchiveMode.Create:
165 _readEntries = true;
166 return;
167 case ZipArchiveMode.Read:
169 return;
170 }
171 if (_archiveStream.Length == 0L)
172 {
173 _readEntries = true;
174 return;
175 }
178 foreach (ZipArchiveEntry entry in _entries)
179 {
180 entry.ThrowIfNotOpenable(needToUncompress: false, needToLoadIntoMemory: true);
181 }
182 }
183 catch
184 {
185 stream2?.Dispose();
186 throw;
187 }
188 }
189
191 {
192 return DoCreateEntry(entryName, null);
193 }
194
199
200 protected virtual void Dispose(bool disposing)
201 {
202 if (!disposing || _isDisposed)
203 {
204 return;
205 }
206 try
207 {
208 ZipArchiveMode mode = _mode;
209 if (mode != 0)
210 {
211 _ = mode - 1;
212 _ = 1;
213 WriteFile();
214 }
215 }
216 finally
217 {
218 CloseStreams();
219 _isDisposed = true;
220 }
221 }
222
223 public void Dispose()
224 {
225 Dispose(disposing: true);
226 GC.SuppressFinalize(this);
227 }
228
230 {
231 if (entryName == null)
232 {
233 throw new ArgumentNullException("entryName");
234 }
235 if (_mode == ZipArchiveMode.Create)
236 {
238 }
241 return value;
242 }
243
245 {
246 if (entryName == null)
247 {
248 throw new ArgumentNullException("entryName");
249 }
250 if (string.IsNullOrEmpty(entryName))
251 {
252 throw new ArgumentException(System.SR.CannotBeEmpty, "entryName");
253 }
254 if (_mode == ZipArchiveMode.Read)
255 {
257 }
259 ZipArchiveEntry zipArchiveEntry = (compressionLevel.HasValue ? new ZipArchiveEntry(this, entryName, compressionLevel.Value) : new ZipArchiveEntry(this, entryName));
261 return zipArchiveEntry;
262 }
263
276
278 {
280 string fullName = entry.FullName;
282 {
284 }
285 }
286
288 {
289 _archiveStreamOwner = null;
290 }
291
293 {
296 }
297
298 internal void ThrowIfDisposed()
299 {
300 if (_isDisposed)
301 {
302 throw new ObjectDisposedException(GetType().ToString());
303 }
304 }
305
306 private void CloseStreams()
307 {
308 if (!_leaveOpen)
309 {
313 }
314 else if (_backingStream != null)
315 {
317 }
318 }
319
321 {
322 if (!_readEntries)
323 {
325 _readEntries = true;
326 }
327 }
328
329 private void ReadCentralDirectory()
330 {
331 try
332 {
334 long num = 0L;
335 bool saveExtraFieldsAndComments = Mode == ZipArchiveMode.Update;
338 {
339 AddEntry(new ZipArchiveEntry(this, header));
340 num++;
341 }
342 if (num != _expectedNumberOfEntries)
343 {
345 }
346 }
347 catch (EndOfStreamException p)
348 {
350 }
351 }
352
354 {
355 try
356 {
358 if (!ZipHelper.SeekBackwardsToSignature(_archiveStream, 101010256u, 65539))
359 {
361 }
362 long position = _archiveStream.Position;
365 if (eocdBlock.NumberOfThisDisk != eocdBlock.NumberOfTheDiskWithTheStartOfTheCentralDirectory)
366 {
368 }
369 _numberOfThisDisk = eocdBlock.NumberOfThisDisk;
370 _centralDirectoryStart = eocdBlock.OffsetOfStartOfCentralDirectoryWithRespectToTheStartingDiskNumber;
371 if (eocdBlock.NumberOfEntriesInTheCentralDirectory != eocdBlock.NumberOfEntriesInTheCentralDirectoryOnThisDisk)
372 {
374 }
375 _expectedNumberOfEntries = eocdBlock.NumberOfEntriesInTheCentralDirectory;
376 if (_mode == ZipArchiveMode.Update)
377 {
378 _archiveComment = eocdBlock.ArchiveComment;
379 }
382 {
384 }
385 }
387 {
389 }
391 {
393 }
394 }
395
397 {
398 if (eocd.NumberOfThisDisk != ushort.MaxValue && eocd.OffsetOfStartOfCentralDirectoryWithRespectToTheStartingDiskNumber != uint.MaxValue && eocd.NumberOfEntriesInTheCentralDirectory != ushort.MaxValue)
399 {
400 return;
401 }
404 {
407 if (zip64EOCDLocator.OffsetOfZip64EOCD > long.MaxValue)
408 {
410 }
411 long offsetOfZip64EOCD = (long)zip64EOCDLocator.OffsetOfZip64EOCD;
414 {
416 }
417 _numberOfThisDisk = zip64EOCDRecord.NumberOfThisDisk;
418 if (zip64EOCDRecord.NumberOfEntriesTotal > long.MaxValue)
419 {
421 }
422 if (zip64EOCDRecord.OffsetOfCentralDirectory > long.MaxValue)
423 {
425 }
426 if (zip64EOCDRecord.NumberOfEntriesTotal != zip64EOCDRecord.NumberOfEntriesOnThisDisk)
427 {
429 }
430 _expectedNumberOfEntries = (long)zip64EOCDRecord.NumberOfEntriesTotal;
431 _centralDirectoryStart = (long)zip64EOCDRecord.OffsetOfCentralDirectory;
432 }
433 }
434
435 private void WriteFile()
436 {
437 if (_mode == ZipArchiveMode.Update)
438 {
440 foreach (ZipArchiveEntry entry in _entries)
441 {
442 if (!entry.LoadLocalHeaderExtraFieldAndCompressedBytesIfNeeded())
443 {
444 list.Add(entry);
445 }
446 }
447 foreach (ZipArchiveEntry item in list)
448 {
449 item.Delete();
450 }
453 }
454 foreach (ZipArchiveEntry entry2 in _entries)
455 {
456 entry2.WriteAndFinishLocalEntry();
457 }
458 long position = _archiveStream.Position;
459 foreach (ZipArchiveEntry entry3 in _entries)
460 {
461 entry3.WriteCentralDirectoryFileHeader();
462 }
463 long sizeOfCentralDirectory = _archiveStream.Position - position;
465 }
466
477}
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
void Add(TKey key, TValue value)
static void SuppressFinalize(object obj)
Definition GC.cs:202
Definition GC.cs:8
virtual void Dispose(bool disposing)
void WriteArchiveEpilogue(long startOfCentralDirectory, long sizeOfCentralDirectory)
void ReleaseArchiveStream(ZipArchiveEntry entry)
void AcquireArchiveStream(ZipArchiveEntry entry)
void AddEntry(ZipArchiveEntry entry)
virtual void Dispose(bool disposing)
Dictionary< string, ZipArchiveEntry > _entriesDictionary
Definition ZipArchive.cs:21
ZipArchiveEntry CreateEntry(string entryName)
ZipArchive(Stream stream, ZipArchiveMode mode, bool leaveOpen, Encoding? entryNameEncoding)
Definition ZipArchive.cs:94
void TryReadZip64EndOfCentralDirectory(ZipEndOfCentralDirectoryBlock eocd, long eocdStart)
ZipArchiveEntry CreateEntry(string entryName, CompressionLevel compressionLevel)
ZipArchiveEntry _archiveStreamOwner
Definition ZipArchive.cs:11
void RemoveEntry(ZipArchiveEntry entry)
ZipArchiveEntry? GetEntry(string entryName)
ReadOnlyCollection< ZipArchiveEntry > _entriesCollection
Definition ZipArchive.cs:19
List< ZipArchiveEntry > _entries
Definition ZipArchive.cs:17
ReadOnlyCollection< ZipArchiveEntry > Entries
Definition ZipArchive.cs:42
ZipArchive(Stream stream, ZipArchiveMode mode, bool leaveOpen)
Definition ZipArchive.cs:89
ZipArchive(Stream stream, ZipArchiveMode mode)
Definition ZipArchive.cs:84
ZipArchiveEntry DoCreateEntry(string entryName, CompressionLevel? compressionLevel)
static bool SeekBackwardsToSignature(Stream stream, uint signatureToFind, int maxBytesToRead)
Definition ZipHelper.cs:71
void SetLength(long value)
long Seek(long offset, SeekOrigin origin)
void CopyTo(Stream destination)
Definition Stream.cs:540
void Dispose()
Definition Stream.cs:639
static string CentralDirectoryInvalid
Definition SR.cs:56
static string ReadModeCapabilities
Definition SR.cs:110
static string CreateModeCreateEntryWhileOpen
Definition SR.cs:62
static string NumEntriesWrong
Definition SR.cs:106
static string CreateModeCapabilities
Definition SR.cs:60
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string UpdateModeCapabilities
Definition SR.cs:126
static string EOCDNotFound
Definition SR.cs:82
static string FieldTooBigOffsetToCD
Definition SR.cs:90
static string EntryNameEncodingNotSupported
Definition SR.cs:76
static string CannotBeEmpty
Definition SR.cs:52
static string SplitSpanned
Definition SR.cs:118
static string Zip64EOCDNotWhereExpected
Definition SR.cs:132
static string CreateInReadMode
Definition SR.cs:58
static string EntriesInCreateMode
Definition SR.cs:74
static string FieldTooBigOffsetToZip64EOCD
Definition SR.cs:92
static string CDCorrupt
Definition SR.cs:54
static string FieldTooBigNumEntries
Definition SR.cs:88
Definition SR.cs:7
static Encoding BigEndianUnicode
Definition Encoding.cs:521
static Encoding Unicode
Definition Encoding.cs:519
static bool TryReadBlock(BinaryReader reader, out Zip64EndOfCentralDirectoryLocator zip64EOCDLocator)
static void WriteBlock(Stream stream, long zip64EOCDRecordStart)
static void WriteBlock(Stream stream, long numberOfEntries, long startOfCentralDirectory, long sizeOfCentralDirectory)
static bool TryReadBlock(BinaryReader reader, out Zip64EndOfCentralDirectoryRecord zip64EOCDRecord)
static bool TryReadBlock(BinaryReader reader, bool saveExtraFieldsAndComments, out ZipCentralDirectoryFileHeader header)
static bool TryReadBlock(BinaryReader reader, out ZipEndOfCentralDirectoryBlock eocdBlock)
static void WriteBlock(Stream stream, long numberOfEntries, long startOfCentralDirectory, long sizeOfCentralDirectory, byte[] archiveComment)