/// <summary> /// Validates whether the zip is corrupted or not /// </summary> /// <param name="path">Path to the zip file</param> /// <returns>true if archive tests ok; false otherwise.</returns> public static bool ValidateZip(string path) { using (var zip = new ICSharpCode.SharpZipLib.Zip.ZipFile(path)) { return(zip.TestArchive(true)); } }
} // End UnZip /// <summary> /// Unzip a local file and return its contents via streamreader to a local the same location as the ZIP. /// </summary> /// <param name="zipFile">Location of the zip on the HD</param> /// <returns>List of unzipped file names</returns> public static List <string> UnzipToFolder(string zipFile) { //1. Initialize: var files = new List <string>(); var slash = zipFile.LastIndexOf(Path.DirectorySeparatorChar); var outFolder = ""; if (slash > 0) { outFolder = zipFile.Substring(0, slash); } ICSharpCode.SharpZipLib.Zip.ZipFile zf = null; try { var fs = File.OpenRead(zipFile); zf = new ICSharpCode.SharpZipLib.Zip.ZipFile(fs); foreach (ZipEntry zipEntry in zf) { //Ignore Directories if (!zipEntry.IsFile) { continue; } //Remove the folder from the entry var entryFileName = Path.GetFileName(zipEntry.Name); if (entryFileName == null) { continue; } var buffer = new byte[4096]; // 4K is optimum var zipStream = zf.GetInputStream(zipEntry); // Manipulate the output filename here as desired. var fullZipToPath = Path.Combine(outFolder, entryFileName); //Save the file name for later: files.Add(fullZipToPath); //Log.Trace("Data.UnzipToFolder(): Input File: " + zipFile + ", Output Directory: " + fullZipToPath); //Copy the data in buffer chunks using (var streamWriter = File.Create(fullZipToPath)) { StreamUtils.Copy(zipStream, streamWriter, buffer); } } } finally { if (zf != null) { zf.IsStreamOwner = true; // Makes close also shut the underlying stream zf.Close(); // Ensure we release resources } } return(files); } // End UnZip
} // End UnZip /// <summary> /// Unzip a local file and return its contents via streamreader to a local the same location as the ZIP. /// </summary> /// <param name="zipFile">Location of the zip on the HD</param> /// <returns>List of unzipped file names</returns> public static List <string> UnzipToFolder(string zipFile) { //1. Initialize: var files = new List <string>(); var outFolder = Path.GetDirectoryName(zipFile); if (string.IsNullOrEmpty(outFolder)) { outFolder = Directory.GetCurrentDirectory(); } ICSharpCode.SharpZipLib.Zip.ZipFile zf = null; try { var fs = File.OpenRead(zipFile); zf = new ICSharpCode.SharpZipLib.Zip.ZipFile(fs); foreach (ZipEntry zipEntry in zf) { //Ignore Directories if (!zipEntry.IsFile) { continue; } var buffer = new byte[4096]; // 4K is optimum var zipStream = zf.GetInputStream(zipEntry); // Manipulate the output filename here as desired. var fullZipToPath = Path.Combine(outFolder, zipEntry.Name); var targetFile = new FileInfo(fullZipToPath); if (targetFile.Directory != null && !targetFile.Directory.Exists) { targetFile.Directory.Create(); } //Save the file name for later: files.Add(fullZipToPath); //Copy the data in buffer chunks using (var streamWriter = File.Create(fullZipToPath)) { StreamUtils.Copy(zipStream, streamWriter, buffer); } } } catch { // lets catch the exception just to log some information about the zip file Log.Error($"Compression.UnzipToFolder(): Failure: zipFile: {zipFile} - outFolder: {outFolder} - files: {string.Join(",", files)}"); throw; } finally { if (zf != null) { zf.IsStreamOwner = true; // Makes close also shut the underlying stream zf.Close(); // Ensure we release resources } } return(files); } // End UnZip
} // End UnZip /// <summary> /// Unzip a local file and return its contents via streamreader to a local the same location as the ZIP. /// </summary> /// <param name="zipFile">Location of the zip on the HD</param> /// <returns>List of unzipped file names</returns> public static List<string> UnzipToFolder(string zipFile) { //1. Initialize: var files = new List<string>(); var slash = zipFile.LastIndexOf(Path.DirectorySeparatorChar); var outFolder = ""; if (slash > 0) { outFolder = zipFile.Substring(0, slash); } ICSharpCode.SharpZipLib.Zip.ZipFile zf = null; try { var fs = File.OpenRead(zipFile); zf = new ICSharpCode.SharpZipLib.Zip.ZipFile(fs); foreach (ZipEntry zipEntry in zf) { //Ignore Directories if (!zipEntry.IsFile) continue; //Remove the folder from the entry var entryFileName = Path.GetFileName(zipEntry.Name); if (entryFileName == null) continue; var buffer = new byte[4096]; // 4K is optimum var zipStream = zf.GetInputStream(zipEntry); // Manipulate the output filename here as desired. var fullZipToPath = Path.Combine(outFolder, entryFileName); //Save the file name for later: files.Add(fullZipToPath); //Log.Trace("Data.UnzipToFolder(): Input File: " + zipFile + ", Output Directory: " + fullZipToPath); //Copy the data in buffer chunks using (var streamWriter = File.Create(fullZipToPath)) { StreamUtils.Copy(zipStream, streamWriter, buffer); } } } finally { if (zf != null) { zf.IsStreamOwner = true; // Makes close also shut the underlying stream zf.Close(); // Ensure we release resources } } return files; } // End UnZip