private static void UnzipFile(string filePath, string outputPath, string tempFolder) { var zip = new FileInfo(filePath); // if its not a zipped file, assume its already been unzipped if (!zip.Extension.Contains("zip")) { return; } using (var fileStreamIn = FlatFiles.GetS3File(filePath)) using (var zipStreamIn = new ZipInputStream(fileStreamIn)) { ZipEntry entry = null; outputPath = outputPath.EndsWith("\\") ? outputPath : outputPath + "\\"; while ((entry = zipStreamIn.GetNextEntry()) != null) { /* * using (var fileStreamOut = new MemoryTributary()) * { * var data = new byte[2048]; * while (true) * { * var readLength = zipStreamIn.Read(data, 0, data.Length); * if (readLength > 0) * fileStreamOut.Write(data, 0, readLength); * else * break; * } * */ // copy this to s3 var outputFile = string.Format("{0}{1}", outputPath, entry.Name).Replace("\\", "/"); // fileStreamOut.Seek(0, SeekOrigin.Begin); if (!zipStreamIn.CanSeek || zipStreamIn.Length > 0) { FlatFiles.AddS3File(zipStreamIn, outputFile, tempFolder); } else { // just unzipped a 0 bytes file. Don't bother processing and archive it throw new EmptyFileException("Unzipped zile contained no data"); } //} } //fileStreamIn.Close(); } }
private static void UnGzipFile(string filePath, string outputPath, string tempFolder) { var dataBuffer = new byte[4096]; var gzip = new FileInfo(filePath); // if its not a zipped file, assume its already been unzipped if (!gzip.Extension.Contains("gz")) { return; } var tempFile = Path.Combine(tempFolder, Path.GetFileNameWithoutExtension(filePath)); try { using (var fileStreamIn = FlatFiles.GetS3File(filePath)) using (var gzipStreamIn = new GZipInputStream(fileStreamIn)) { using (var fileStreamOut = File.Create(tempFile)) { // copy unzipped file locally temporily StreamUtils.Copy(gzipStreamIn, fileStreamOut, dataBuffer); } } using (var tempFileStream = new FileStream(tempFile, FileMode.Open, FileAccess.Read)) { // upload unzipped version to s3 var tempFileInfo = new FileInfo(tempFile); outputPath = outputPath.EndsWith("\\") ? outputPath : outputPath + "\\"; var outputFile = string.Format("{0}{1}", outputPath, tempFileInfo.Name).Replace("\\", "/"); var key = outputFile.Replace("\\", "/"); FlatFiles.AddS3File(tempFileStream, key, tempFolder); } } finally { // clean up temp file if (File.Exists(tempFile)) { File.Delete(tempFile); } } }