public override void Finish() { if (this.entries != null) { if (this.curEntry != null) { this.CloseEntry(); } long count = this.entries.Count; long sizeEntries = 0L; foreach (ZipEntry entry in this.entries) { this.WriteLeInt(0x2014b50); this.WriteLeShort(0x33); this.WriteLeShort(entry.Version); this.WriteLeShort(entry.Flags); this.WriteLeShort((short) entry.CompressionMethodForHeader); this.WriteLeInt((int) entry.DosTime); this.WriteLeInt((int) entry.Crc); if (entry.IsZip64Forced() || (entry.CompressedSize >= 0xffffffffL)) { this.WriteLeInt(-1); } else { this.WriteLeInt((int) entry.CompressedSize); } if (entry.IsZip64Forced() || (entry.Size >= 0xffffffffL)) { this.WriteLeInt(-1); } else { this.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."); } this.WriteLeShort(buffer.Length); this.WriteLeShort(entryData.Length); this.WriteLeShort(buffer3.Length); this.WriteLeShort(0); this.WriteLeShort(0); if (entry.ExternalFileAttributes != -1) { this.WriteLeInt(entry.ExternalFileAttributes); } else if (entry.IsDirectory) { this.WriteLeInt(0x10); } else { this.WriteLeInt(0); } if (entry.Offset >= 0xffffffffL) { this.WriteLeInt(-1); } else { this.WriteLeInt((int) entry.Offset); } if (buffer.Length > 0) { base.baseOutputStream_.Write(buffer, 0, buffer.Length); } if (entryData.Length > 0) { base.baseOutputStream_.Write(entryData, 0, entryData.Length); } if (buffer3.Length > 0) { base.baseOutputStream_.Write(buffer3, 0, buffer3.Length); } sizeEntries += ((0x2e + buffer.Length) + entryData.Length) + buffer3.Length; } using (ZipHelperStream stream = new ZipHelperStream(base.baseOutputStream_)) { stream.WriteEndOfCentralDirectory(count, sizeEntries, this.offset, this.zipComment); } this.entries = null; } }
3.ZipEntry类Clone():
public object Clone() { ZipEntry entry = (ZipEntry) base.MemberwiseClone(); if (this.extra != null) { entry.extra = new byte[this.extra.Length]; Array.Copy(this.extra, 0, entry.extra, 0, this.extra.Length); } return entry; }
4.ZipOutputStream类Write():
public override void Write(byte[] buffer, int offset, int count) { if (this.curEntry == null) { throw new InvalidOperationException("No open entry."); } if (buffer == null) { throw new ArgumentNullException("buffer"); } if (offset < 0) { throw new ArgumentOutOfRangeException("offset", "Cannot be negative"); } if (count < 0) { throw new ArgumentOutOfRangeException("count", "Cannot be negative"); } if ((buffer.Length - offset) < count) { throw new ArgumentException("Invalid offset/count combination"); } this.crc.Update(buffer, offset, count); this.size += count; switch (this.curMethod) { case CompressionMethod.Stored: if (base.Password != null) { this.CopyAndEncrypt(buffer, offset, count); } else { base.baseOutputStream_.Write(buffer, offset, count); } break; case CompressionMethod.Deflated: base.Write(buffer, offset, count); break; } }
三.SharpZipLib实例:
1.压缩单个文件: