void RunUpdates() { long sizeEntries = 0; long endOfStream = 0; bool allOk = true; bool directUpdate = false; long destinationPosition = 0; // NOT SFX friendly ZipFile workFile; if ( IsNewArchive ) { workFile = this; workFile.baseStream_.Position = 0; directUpdate = true; } else if ( archiveStorage_.UpdateMode == FileUpdateMode.Direct ) { workFile = this; workFile.baseStream_.Position = 0; directUpdate = true; // Sort the updates by offset within copies/modifies, then adds. // This ensures that copies will not overwrite any required data. updates_.Sort(new UpdateComparer()); } else { workFile = ZipFile.Create(archiveStorage_.GetTemporaryOutput()); if (key != null) { workFile.key = (byte[])key.Clone(); } } try { foreach ( ZipUpdate update in updates_ ) { switch ( update.Command ) { case UpdateCommand.Copy: if ( directUpdate ) { CopyEntryDirect(workFile, update, ref destinationPosition); } else { CopyEntry(workFile, update); } break; case UpdateCommand.Modify: // TODO: Direct modifying of an entry. ModifyEntry(workFile, update); break; case UpdateCommand.Add: if ( !IsNewArchive && directUpdate ) { workFile.baseStream_.Position = destinationPosition; } AddEntry(workFile, update); if ( directUpdate ) { destinationPosition = workFile.baseStream_.Position; } break; } } if ( !IsNewArchive && directUpdate ) { workFile.baseStream_.Position = destinationPosition; } long centralDirOffset = workFile.baseStream_.Position; foreach ( ZipUpdate update in updates_ ) { sizeEntries += workFile.WriteCentralDirectoryHeader(update.OutEntry); } byte[] theComment = (newComment_ != null) ? newComment_.RawComment : ZipConstants.ConvertToArray(comment_); using ( ZipHelperStream zhs = new ZipHelperStream(workFile.baseStream_) ) { zhs.WriteEndOfCentralDirectory(updates_.Count, sizeEntries, centralDirOffset, theComment); } endOfStream = workFile.baseStream_.Position; // And now patch entries... foreach ( ZipUpdate update in updates_ ) { // If the size of the entry is zero leave the crc as 0 as well. // The calculated crc will be all bits on... if ( (update.CrcPatchOffset > 0) && (update.OutEntry.CompressedSize > 0) ) { workFile.baseStream_.Position = update.CrcPatchOffset; workFile.WriteLEInt(( int )update.OutEntry.Crc); } if ( update.SizePatchOffset > 0 ) { workFile.baseStream_.Position = update.SizePatchOffset; if ( update.Entry.LocalHeaderRequiresZip64 ) { workFile.WriteLeLong(update.OutEntry.Size); workFile.WriteLeLong(update.OutEntry.CompressedSize); } else { workFile.WriteLEInt(( int )update.OutEntry.CompressedSize); workFile.WriteLEInt(( int )update.OutEntry.Size); } } } } catch(Exception) { allOk = false; } finally { if ( directUpdate ) { if ( allOk ) { workFile.baseStream_.Flush(); workFile.baseStream_.SetLength(endOfStream); } } else { workFile.Close(); } } if ( allOk ) { if ( directUpdate ) { isNewArchive_ = false; workFile.baseStream_.Flush(); ReadEntries(); } else { baseStream_.Close(); Reopen(archiveStorage_.ConvertTemporaryToFinal()); } } else { workFile.Close(); if ( !directUpdate && (workFile.Name != null) ) { File.Delete(workFile.Name); } } }
/// <summary> /// Finishes the stream. This will write the central directory at the /// end of the zip file and flush the stream. /// </summary> /// <remarks> /// This is automatically called when the stream is closed. /// </remarks> /// <exception cref="System.IO.IOException"> /// An I/O error occurs. /// </exception> /// <exception cref="ZipException"> /// Comment exceeds the maximum length<br/> /// Entry name exceeds the maximum length /// </exception> public override void Finish() { if (entries == null) { return; } if (curEntry != null) { CloseEntry(); } long numEntries = entries.Count; long sizeEntries = 0; foreach (ZipEntry entry in entries) { WriteLeInt(ZipConstants.CentralHeaderSignature); WriteLeShort(ZipConstants.VersionMadeBy); WriteLeShort(entry.Version); WriteLeShort(entry.Flags); WriteLeShort((short)entry.CompressionMethod); WriteLeInt((int)entry.DosTime); WriteLeInt((int)entry.Crc); if ( entry.IsZip64Forced() || (entry.CompressedSize >= uint.MaxValue) ) { WriteLeInt(-1); } else { WriteLeInt((int)entry.CompressedSize); } if ( entry.IsZip64Forced() || (entry.Size >= uint.MaxValue) ) { WriteLeInt(-1); } else { WriteLeInt((int)entry.Size); } byte[] name = ZipConstants.ConvertToArray(entry.Flags, entry.Name); if (name.Length > 0xffff) { throw new ZipException("Name too long."); } ZipExtraData ed = new ZipExtraData(entry.ExtraData); if ( entry.CentralHeaderRequiresZip64 ) { ed.StartNewEntry(); if ( entry.IsZip64Forced() || (entry.Size >= 0xffffffff) ) { ed.AddLeLong(entry.Size); } if ( entry.IsZip64Forced() || (entry.CompressedSize >= 0xffffffff) ) { ed.AddLeLong(entry.CompressedSize); } if ( entry.Offset >= 0xffffffff ) { ed.AddLeLong(entry.Offset); } ed.AddNewEntry(1); } else { ed.Delete(1); } byte[] extra = ed.GetEntryData(); byte[] entryComment = (entry.Comment != null) ? ZipConstants.ConvertToArray(entry.Flags, entry.Comment) : new byte[0]; if (entryComment.Length > 0xffff) { throw new ZipException("Comment too long."); } WriteLeShort(name.Length); WriteLeShort(extra.Length); WriteLeShort(entryComment.Length); WriteLeShort(0); // disk number WriteLeShort(0); // internal file attributes // external file attributes if (entry.ExternalFileAttributes != -1) { WriteLeInt(entry.ExternalFileAttributes); } else { if (entry.IsDirectory) { // mark entry as directory (from nikolam.AT.perfectinfo.com) WriteLeInt(16); } else { WriteLeInt(0); } } if ( entry.Offset >= uint.MaxValue ) { WriteLeInt(-1); } else { WriteLeInt((int)entry.Offset); } if ( name.Length > 0 ) { baseOutputStream_.Write(name, 0, name.Length); } if ( extra.Length > 0 ) { baseOutputStream_.Write(extra, 0, extra.Length); } if ( entryComment.Length > 0 ) { baseOutputStream_.Write(entryComment, 0, entryComment.Length); } sizeEntries += ZipConstants.CentralHeaderBaseSize + name.Length + extra.Length + entryComment.Length; } using ( ZipHelperStream zhs = new ZipHelperStream(baseOutputStream_) ) { zhs.WriteEndOfCentralDirectory(numEntries, sizeEntries, offset, zipComment); } entries = null; }
/// <summary> /// Commit current updates, updating this archive. /// </summary> /// <seealso cref="BeginUpdate()"></seealso> /// <seealso cref="AbortUpdate"></seealso> public void CommitUpdate() { CheckUpdating(); if ( contentsEdited_ ) { RunUpdates(); } else if ( commentEdited_ ) { UpdateCommentOnly(); } else { // Create an empty archive if none existed originally. if ( (entries_ != null) && (entries_.Length == 0) ) { byte[] theComment = (newComment_ != null) ? newComment_.RawComment : ZipConstants.ConvertToArray(comment_); using ( ZipHelperStream zhs = new ZipHelperStream(baseStream_) ) { zhs.WriteEndOfCentralDirectory(0, 0, 0, theComment); } } } PostUpdateCleanup(); }
public override void Finish() { if (_entries != null) { if (_curEntry != null) { CloseEntry(); } long count = _entries.Count; long sizeEntries = 0L; foreach (ZipEntry entry in _entries) { WriteLeInt(ZipConstants.CentralHeaderSignature); WriteLeShort(0x33); WriteLeShort(entry.Version); WriteLeShort(entry.Flags); WriteLeShort((short)entry.CompressionMethodForHeader); WriteLeInt((int)entry.DosTime); WriteLeInt((int)entry.Crc); if (entry.IsZip64Forced() || (entry.CompressedSize >= 0xffffffffL)) { WriteLeInt(-1); } else { WriteLeInt((int)entry.CompressedSize); } if (entry.IsZip64Forced() || (entry.Size >= 0xffffffffL)) { WriteLeInt(-1); } else { WriteLeInt((int)entry.Size); } byte[] buffer = ZipConstants.ConvertToArray(entry.Flags, entry.Name); if (buffer.Length > 0xffff) { throw new ZipException("Name too long."); } ZipExtraData extraData = new ZipExtraData(entry.ExtraData); if (entry.CentralHeaderRequiresZip64) { extraData.StartNewEntry(); if (entry.IsZip64Forced() || (entry.Size >= 0xffffffffL)) { extraData.AddLeLong(entry.Size); } if (entry.IsZip64Forced() || (entry.CompressedSize >= 0xffffffffL)) { extraData.AddLeLong(entry.CompressedSize); } if (entry.Offset >= 0xffffffffL) { extraData.AddLeLong(entry.Offset); } extraData.AddNewEntry(1); } else { extraData.Delete(1); } if (entry.AesKeySize > 0) { AddExtraDataAes(entry, extraData); } byte[] entryData = extraData.GetEntryData(); byte[] buffer3 = (entry.Comment != null) ? ZipConstants.ConvertToArray(entry.Flags, entry.Comment) : new byte[0]; if (buffer3.Length > 0xffff) { throw new ZipException("Comment too long."); } WriteLeShort(buffer.Length); WriteLeShort(entryData.Length); WriteLeShort(buffer3.Length); WriteLeShort(0); WriteLeShort(0); if (entry.ExternalFileAttributes != -1) { WriteLeInt(entry.ExternalFileAttributes); } else if (entry.IsDirectory) { WriteLeInt(0x10); } else { WriteLeInt(0); } if (entry.Offset >= 0xffffffffL) { WriteLeInt(-1); } else { WriteLeInt((int)entry.Offset); } if (buffer.Length > 0) { BaseOutputStream.Write(buffer, 0, buffer.Length); } if (entryData.Length > 0) { BaseOutputStream.Write(entryData, 0, entryData.Length); } if (buffer3.Length > 0) { BaseOutputStream.Write(buffer3, 0, buffer3.Length); } sizeEntries += ((ZipConstants.CentralHeaderBaseSize + buffer.Length) + entryData.Length) + buffer3.Length; } using (ZipHelperStream stream = new ZipHelperStream(BaseOutputStream)) { stream.WriteEndOfCentralDirectory(count, sizeEntries, _offset, _zipComment); } _entries = null; } }
/// <summary> /// Finishes the stream. This will write the central directory at the /// end of the zip file and flush the stream. /// </summary> /// <remarks> /// This is automatically called when the stream is closed. /// </remarks> /// <exception cref="System.IO.IOException"> /// An I/O error occurs. /// </exception> /// <exception cref="ZipException"> /// Comment exceeds the maximum length<br/> /// Entry name exceeds the maximum length /// </exception> public override void Finish() { if (entries == null) { return; } if (curEntry != null) { CloseEntry(); } long numEntries = entries.Count; long sizeEntries = 0; foreach (ZipEntry entry in entries) { WriteLeInt(ZipConstants.CentralHeaderSignature); WriteLeShort(ZipConstants.VersionMadeBy); WriteLeShort(entry.Version); WriteLeShort(entry.Flags); WriteLeShort((short)entry.CompressionMethod); WriteLeInt((int)entry.DosTime); WriteLeInt((int)entry.Crc); if (entry.IsZip64Forced() || (entry.CompressedSize >= uint.MaxValue)) { WriteLeInt(-1); } else { WriteLeInt((int)entry.CompressedSize); } if (entry.IsZip64Forced() || (entry.Size >= uint.MaxValue)) { WriteLeInt(-1); } else { WriteLeInt((int)entry.Size); } byte[] name = ZipConstants.ConvertToArray(entry.Flags, entry.Name); if (name.Length > 0xffff) { throw new ZipException("Name too long."); } ZipExtraData ed = new ZipExtraData(entry.ExtraData); if (entry.CentralHeaderRequiresZip64) { ed.StartNewEntry(); if (entry.IsZip64Forced() || (entry.Size >= 0xffffffff)) { ed.AddLeLong(entry.Size); } if (entry.IsZip64Forced() || (entry.CompressedSize >= 0xffffffff)) { ed.AddLeLong(entry.CompressedSize); } if (entry.Offset >= 0xffffffff) { ed.AddLeLong(entry.Offset); } ed.AddNewEntry(1); } else { ed.Delete(1); } byte[] extra = ed.GetEntryData(); byte[] entryComment = (entry.Comment != null) ? ZipConstants.ConvertToArray(entry.Flags, entry.Comment) : new byte[0]; if (entryComment.Length > 0xffff) { throw new ZipException("Comment too long."); } WriteLeShort(name.Length); WriteLeShort(extra.Length); WriteLeShort(entryComment.Length); WriteLeShort(0); // disk number WriteLeShort(0); // internal file attributes // external file attributes if (entry.ExternalFileAttributes != -1) { WriteLeInt(entry.ExternalFileAttributes); } else { if (entry.IsDirectory) // mark entry as directory (from nikolam.AT.perfectinfo.com) { WriteLeInt(16); } else { WriteLeInt(0); } } if (entry.Offset >= uint.MaxValue) { WriteLeInt(-1); } else { WriteLeInt((int)entry.Offset); } if (name.Length > 0) { baseOutputStream.Write(name, 0, name.Length); } if (extra.Length > 0) { baseOutputStream.Write(extra, 0, extra.Length); } if (entryComment.Length > 0) { baseOutputStream.Write(entryComment, 0, entryComment.Length); } sizeEntries += ZipConstants.CentralHeaderBaseSize + name.Length + extra.Length + entryComment.Length; } using (ZipHelperStream zhs = new ZipHelperStream(baseOutputStream)) { zhs.WriteEndOfCentralDirectory(numEntries, sizeEntries, offset, zipComment); } entries = null; }
public void CommitUpdate() { if (_isDisposed) { throw new ObjectDisposedException("ZipFile"); } CheckUpdating(); try { _updateIndex.Clear(); _updateIndex = null; if (_contentsEdited) { RunUpdates(); } else if (_commentEdited) { UpdateCommentOnly(); } else if (_entries.Length == 0) { var comment = (_newComment != null) ? _newComment.RawComment : ZipConstants.ConvertToArray(_comment); using (var stream = new ZipHelperStream(_baseStream)) { stream.WriteEndOfCentralDirectory(0L, 0L, 0L, comment); } } } finally { PostUpdateCleanup(); } }
private void RunUpdates() { ZipFile file; var sizeEntries = 0L; long num2; var flag = false; var destinationPosition = 0L; if (IsNewArchive) { file = this; file._baseStream.Position = 0L; flag = true; } else if (_archiveStorage.UpdateMode == FileUpdateMode.Direct) { file = this; file._baseStream.Position = 0L; flag = true; _updates.Sort(new UpdateComparer()); } else { file = Create(_archiveStorage.GetTemporaryOutput()); file.UseZip64 = UseZip64; if (_key != null) { file._key = (byte[])_key.Clone(); } } try { foreach (ZipUpdate update in _updates) { if (update != null) { switch (update.Command) { case UpdateCommand.Copy: if (!flag) { goto Label_00EC; } CopyEntryDirect(file, update, ref destinationPosition); break; case UpdateCommand.Modify: ModifyEntry(file, update); break; case UpdateCommand.Add: goto Label_0104; } } continue; Label_00EC: CopyEntry(file, update); continue; Label_0104: if (!IsNewArchive && flag) { file._baseStream.Position = destinationPosition; } AddEntry(file, update); if (flag) { destinationPosition = file._baseStream.Position; } } if (!IsNewArchive && flag) { file._baseStream.Position = destinationPosition; } var position = file._baseStream.Position; foreach (ZipUpdate update2 in _updates) { if (update2 != null) { sizeEntries += file.WriteCentralDirectoryHeader(update2.OutEntry); } } var comment = (_newComment != null) ? _newComment.RawComment : ZipConstants.ConvertToArray(_comment); using (var stream = new ZipHelperStream(file._baseStream)) { stream.WriteEndOfCentralDirectory(_updateCount, sizeEntries, position, comment); } num2 = file._baseStream.Position; foreach (ZipUpdate update3 in _updates) { if (update3 != null) { if ((update3.CrcPatchOffset > 0L) && (update3.OutEntry.CompressedSize > 0L)) { file._baseStream.Position = update3.CrcPatchOffset; file.WriteLeInt((int)update3.OutEntry.Crc); } if (update3.SizePatchOffset > 0L) { file._baseStream.Position = update3.SizePatchOffset; if (update3.OutEntry.LocalHeaderRequiresZip64) { file.WriteLeLong(update3.OutEntry.Size); file.WriteLeLong(update3.OutEntry.CompressedSize); } else { file.WriteLeInt((int)update3.OutEntry.CompressedSize); file.WriteLeInt((int)update3.OutEntry.Size); } } } } } catch { file.Close(); if (!flag && (file.Name != null)) { File.Delete(file.Name); } throw; } if (flag) { file._baseStream.SetLength(num2); file._baseStream.Flush(); _isNewArchive = false; ReadEntries(); } else { _baseStream.Close(); Reopen(_archiveStorage.ConvertTemporaryToFinal()); } }