public override void AddEntry(string relativePath) { string resolvedPath = ZipLib.ResolvePath(relativePath); ZipFileEntryInfo info; info.DateTime = DateTime.Now; if (this.entryOpened) { ZipLib.zipCloseFileInZip(this.handle); this.entryOpened = false; } int result = ZipLib.zipOpenNewFileInZip(this.handle, resolvedPath, out info, null, 0, null, 0, String.Empty, (int)CompressionMethod.Deflated, (int)CompressionLevel.Default); if (result < 0) { throw new ZipException("Error while opening entry for writing: " + relativePath + " - Errorcode: " + result); } this.entryOpened = true;//经典,用此标志来标示哪个文件当前可写 }
public override void Close() { if (handle != IntPtr.Zero) { int result; if (this.entryOpened) { result = ZipLib.zipCloseFileInZip(this.handle); if (result != 0) { throw new ZipException("Error while closing entry - Errorcode: " + result); } this.entryOpened = false; } result = ZipLib.zipClose(this.handle, ""); handle = IntPtr.Zero; // Should we raise this exception ? if (result != 0) { throw new ZipException("Error while closing ZIP file - Errorcode: " + result); } } }