void SaveArchive(string url, byte[] buffer) { string tempPath = Path.ChangeExtension(StoragePath, "tmp"); // Create a new ZIP archive. using (InternalZipArchive arch = new InternalZipArchive(tempPath)) { // Open a ZIP archive where report files are stored. using (ZipFilesHelper helper = new ZipFilesHelper(StoragePath)) { bool added = false; // Copy all report files to a new archive. // Update a file with a specified URL. // If the file does not exist, create it. foreach (InternalZipFile item in helper.ZipFiles) { if (StringsEgual(item.FileName, url)) { arch.Add(item.FileName, DateTime.Now, buffer); added = true; } else { arch.Add(item.FileName, DateTime.Now, GetBytes(item)); } } if (!added) { arch.Add(url, DateTime.Now, buffer); } } } // Replace the old ZIP archive with the new one. if (File.Exists(StoragePath)) { File.Delete(StoragePath); } File.Move(tempPath, StoragePath); }
public static void CompressDir(string sourceDir, string targetFile) { if (!Directory.Exists(sourceDir)) { throw new ArgumentException("sourceDir"); } using (InternalZipArchive zipper = new InternalZipArchive(targetFile)) { DirectoryInfo di = new DirectoryInfo(sourceDir); foreach (FileInfo fi in di.GetFiles()) { using (FileStream fs = fi.OpenRead()) { zipper.Add(fi.Name, DateTime.Now, fs); } } } }