/// <summary> /// Extract the contents of a zip to a destination directory. /// </summary> protected UnzipResult ExtractZipContents(ZipFile zipFile, string zipName, string destinationDirectory) { UnzipResult result = new UnzipResult(); foreach (ZipEntry zipEntry in zipFile) { if (zipEntry.IsFile) { result.FullUncompressedFileCount++; result.FullUncompressedSize += zipEntry.Size; } if (QualifiesForExtraction(zipEntry, destinationDirectory)) { bool isItemAnArchive = IsSupportedArchiveType(zipEntry); if (isItemAnArchive) { Log.InfoFormat("Extracting nested archive '{0}' from '{1}'.. ({2})", Path.GetFileName(zipEntry.Name), zipName, zipEntry.CompressedSize.ToPrettySize(decimalPlaces: 1)); } string extractedItemLocation = ExtractFileFromZip(zipFile, zipEntry, destinationDirectory); result.ExtractedFileCount++; result.ExtractedSize += zipEntry.Size; if (isItemAnArchive && Strategy.UnzipNestedArchives) { string destinationSubDirectory = Path.Combine(destinationDirectory, Path.GetFileNameWithoutExtension(zipEntry.Name)); UnzipResult nestedResult = ExtractArchive(extractedItemLocation, destinationSubDirectory, deleteOnFinish: true); // Combine tallied metrics with those of nested result. result.FullUncompressedFileCount += nestedResult.FullUncompressedFileCount - 1; result.FullUncompressedSize += nestedResult.FullUncompressedSize - zipEntry.Size; result.ExtractedFileCount += nestedResult.ExtractedFileCount - 1; result.ExtractedSize += nestedResult.ExtractedSize - zipEntry.Size; } } } return(result); }
/// <summary> /// Extracts a .zip archive and tallies the work done. /// </summary> protected UnzipResult ExtractZip(Stream zipStream, string zipName, string destinationDirectory) { ZipFile zipFile = new ZipFile(zipStream) { IsStreamOwner = true }; try { UnzipResult result = ExtractZipContents(zipFile, zipName, destinationDirectory); result.CompressedSize = zipFile.Cast <ZipEntry>().Sum(zipEntry => zipEntry.CompressedSize); Log.InfoFormat("Extracted {0} files from '{1}'. ({2} unpacked)", result.ExtractedFileCount, zipName, result.ExtractedSize.ToPrettySize(decimalPlaces: 1)); return(result); } finally { zipFile.Close(); } }
/// <summary> /// Unpacks a set of archives into a given directory. /// </summary> protected void UnpackArchives(IEnumerable<string> archivesToUnpack, string unpackDirectory) { foreach (string archiveToUnpack in archivesToUnpack) { if (request.Target.IsDirectory) { // When the target is a nested archive, we need to unpack it to a subdirectory. unpackDirectory = Path.Combine(unpackDirectory, Path.GetFileNameWithoutExtension(archiveToUnpack)); } // Extract archive. var unzipStrategy = new UnzipStrategy(WhitelistPatterns, unzipNestedArchives: true); var unzipper = new LogsetUnzipper(unzipStrategy, request); UnzipResult result = unzipper.Unzip(archiveToUnpack, unpackDirectory, deleteOnFinish: request.Target.IsDirectory); // Update target size to include the size of the zip contents (but not the zip itself if it's a nested zip in a directory). request.Target.UncompressedSize += result.FullUncompressedSize; if (request.Target.IsDirectory) { request.Target.UncompressedSize -= result.CompressedSize; } }; }