2021年3月2日星期二

Zip S3 files using C#

What I'm looking for is zip/compress S3 files without having them first downloaded to EFS or on a file system and then upload the zip file back to S3. Is there a C# way to achieve the same? I found the following post, but not sure its C# equivalent https://www.antstack.io/blog/create-zip-using-lambda-with-files-streamed-from-s3/

I've written following code to zip files from a MemoryStream

public static void CreateZip(string zipFileName, List<FileInfo> filesToZip)     {          //zipFileName is the final zip file name          LambdaLogger.Log($"Zipping in progress for: {zipFileName}");          using (MemoryStream zipMS = new MemoryStream())          {              using (ZipArchive zipArchive = new ZipArchive(zipMS, ZipArchiveMode.Create, true))              {                  //loop through files to add                  foreach (var fileToZip in filesToZip)                  {                      //read the file bytes                      byte[] fileToZipBytes = File.ReadAllBytes(fileToZip.FullName);                      ZipArchiveEntry zipFileEntry = zipArchive.CreateEntry(fileToZip.Name);                      //add the file contents                      using (Stream zipEntryStream = zipFileEntry.Open())                      using (BinaryWriter zipFileBinary = new BinaryWriter(zipEntryStream))                      {                          zipFileBinary.Write(fileToZipBytes);                      }                 }              }              using (FileStream finalZipFileStream = new FileStream(zipFileName, FileMode.Create))              {                  zipMS.Seek(0, SeekOrigin.Begin);                  zipMS.CopyTo(finalZipFileStream);              }          }     }  

But problem is how to make it read file directly from S3 and upload the compressed file.

https://stackoverflow.com/questions/66437245/zip-s3-files-using-c-sharp March 02, 2021 at 06:06PM

没有评论:

发表评论