/// <summary> /// compress a directory /// </summary> /// <param name="archive"></param> /// <param name="directoryName"></param> public static void CompressDirectory(ZipFileInfo archive, string directoryName) { using (var zip = new ZipFile(Encoding.UTF8)) { SetZipFile(zip, archive); zip.AddDirectory(directoryName); zip.Save(archive.Filename); } }
/// <summary> /// update archive file to use file or directory as item /// </summary> /// <param name="existingArchive"></param> /// <param name="addFileOrDirectory"></param> /// <param name="directoryPathInArchive"></param> public static void UpdateArchiveItem(ZipFileInfo existingArchive, string addFileOrDirectory, string directoryPathInArchive) { using (var zip = ZipFile.Read(existingArchive.Filename)) { zip.UseZip64WhenSaving = Zip64Option.AsNecessary; zip.UpdateItem(addFileOrDirectory, directoryPathInArchive); zip.Save(); } }
private static void SetZipFile(ZipEntry zipEntry, ZipFileInfo file) { if (!string.IsNullOrEmpty(file.Password)) zipEntry.Password = file.Password; zipEntry.Comment = file.Comment; zipEntry.CompressionMethod = (Ionic.Zip.CompressionMethod)((int)file.CompressionMethod); zipEntry.CompressionLevel = (Ionic.Zlib.CompressionLevel)((int)file.CompressionLevel); }
/// <summary> /// /// </summary> /// <param name="existingArchive"></param> /// <param name="filePathInArchive"></param> /// <returns></returns> public static bool ExistItemInArchive(ZipFileInfo existingArchive, string filePathInArchive) { using (var zip = ZipFile.Read(existingArchive.Filename)) { zip.UseZip64WhenSaving = Zip64Option.AsNecessary; var entryQuery = zip.Entries.Where(o => o.FileName.Equals(filePathInArchive)); if (!entryQuery.Any()) return false; return true; } }
private static void SetZipFile(ZipEntry zipEntry, ZipFileInfo file) { if (!string.IsNullOrEmpty(file.Password)) { zipEntry.Password = file.Password; } zipEntry.Comment = file.Comment; zipEntry.CompressionMethod = (Ionic.Zip.CompressionMethod)((int)file.CompressionMethod); zipEntry.CompressionLevel = (Ionic.Zlib.CompressionLevel)((int)file.CompressionLevel); }
/// <summary> /// compress a file to archive file /// </summary> /// <param name="archive"></param> /// <param name="directoryName"></param> /// <param name="directoryPathInArchive"></param> public static void CompressAndSpecifyPathArchive(ZipFileInfo archive, string directoryName, string directoryPathInArchive) { using (var zip = new ZipFile(Encoding.UTF8)) { SetZipFile(zip, archive); ZipEntry entry = zip.AddDirectory(directoryName, directoryPathInArchive); SetZipFile(entry, archive); zip.Save(archive.Filename); } }
/// <summary> /// /// </summary> /// <param name="archive"></param> /// <param name="stream"></param> /// <param name="streamFile"></param> public static void AddStreamToArchive(ZipFileInfo archive, ZipFileInfo streamFile, Stream stream) { using (var zip = new ZipFile(Encoding.UTF8)) { SetZipFile(zip, archive); ZipEntry entry = zip.AddEntry(streamFile.Filename, stream); SetZipFile(entry, streamFile); zip.Save(archive.Filename); } }
/// <summary> /// compress any files /// </summary> /// <param name="archive"></param> /// <param name="fileInfos"></param> public static void CompressFile(ZipFileInfo archive, List<ZipFileInfo> fileInfos, string directoryPathInArchive) { using (var zip = new ZipFile(Encoding.UTF8)) { SetZipFile(zip, archive); foreach (var file in fileInfos) { SetZipFile(zip, file); zip.AddFile(file.Filename, directoryPathInArchive); } zip.Save(archive.Filename); } }
/// <summary> /// compress any files /// </summary> /// <param name="archive"></param> /// <param name="fileInfos"></param> public static void CompressFile(ZipFileInfo archive, List <ZipFileInfo> fileInfos, string directoryPathInArchive) { using (var zip = new ZipFile(Encoding.UTF8)) { SetZipFile(zip, archive); foreach (var file in fileInfos) { SetZipFile(zip, file); zip.AddFile(file.Filename, directoryPathInArchive); } zip.Save(archive.Filename); } }
/// <summary> /// /// </summary> /// <param name="existingArchive"></param> /// <param name="filePathInArchive"></param> /// <returns></returns> public static bool ExistItemInArchive(ZipFileInfo existingArchive, string filePathInArchive) { using (var zip = ZipFile.Read(existingArchive.Filename)) { zip.UseZip64WhenSaving = Zip64Option.AsNecessary; var entryQuery = zip.Entries.Where(o => o.FileName.Equals(filePathInArchive)); if (!entryQuery.Any()) { return(false); } return(true); } }