Search code examples
c#.netzipmemorystreamc#-ziparchive

Save ZipArchive to Stream


I am working on a project where i update a ZipArchive and upload it back to server but I can only upload it using a stream.

public IActionResult DeleteZippedFile(string path)
{
    //read the zip from stream
    var zip = new ZipArchive(DownloadStream(Request.Cookies["OpenedZipPath"]), ZipArchiveMode.Update);

    //make changes
    zip.Entries.Where(x => x.Name == path).ToList()[0].Delete();

    //i want to convert here <------
    ftp.UploadStream(THESTREAMIWANT)

    //and back to the zip
    return RedirectToAction(nameof(OpenZip));
}

How can i save ZipArchive to a stream?


Solution

  • I think this is impossible without a third party library, so i used the Aspose.zip lib. And here is the working code:

    public IActionResult DeleteZippedFile(string path)
    {
        var zip = new Archive(DownloadStream(Request.Cookies["OpenedZipPath"]));
        zip.DeleteEntry(zip.Entries.FirstOrDefault(x => x.Name == path));
        var ms = new MemoryStream();
        zip.Save(ms);
        ms.Seek(0, SeekOrigin.Begin);
        ftp.UploadStream(ms, Request.Cookies["OpenedZipPath"]);
        return RedirectToAction(nameof(OpenZip));
    }