0 votes
by (250 points)
edited

I tried to create an archive on a FTP Stream which is Writable but not seekable. It throws an error stating that, 'The Stream is not seekable'. Is it necessary that the FTP stream should seekable for creating the zip archive?

1 Answer

0 votes
by (70.2k points)
edited
 
Best answer

The ZipArchive can work with seekable streams only. But you can use e.g. MemoryStream to prepare a ZIP archive in memory, then copy it to a non-seekable stream as follows:

MemoryStream ms = new MemoryStream();

// create ZipArchive in memory
// use LeaveOpen argument to keep MemoryStream usable after closing the ZipArchive
using (ZipArchive zip = new ZipArchive(ms, ArchiveStreamCloseMode.LeaveOpen))
{
    // work with the ZipArchive ...
    zip.Add("C:/temp/data");
}

// copy MemoryStream to any writable stream (not necessary seekable)
using (FileStream fs = new FileStream("C:/temp/data.zip", FileMode.Create))
{
    ms.WriteTo(fs);
}
...