There is a directory from the contents of which you want to create an archive. In this case, the archive should be created in the stream, and not be saved as a file on disk.

There is a ZipFile.CreateFromDirectory(string sourceDirectoryName, string destinationArchiveFileName) method ZipFile.CreateFromDirectory(string sourceDirectoryName, string destinationArchiveFileName) , but it saves the result to a file. Is there anything similar but saving the result to the stream?

It is desirable that these be classes from the System.IO.Compression namespace, i.e. do without third-party libraries.

  • OutOfMemory, when a folder in memory does not fit, you are satisfied? Archiving a folder is still an unobvious process, unlike a file. - Monk
  • @Monk, quite satisfied. - Vlad
  • one
    codeproject.com/Tips/319438/… - only in the packing of the folder, instead of using the stream, use the one you need, well, and do not close it. - Monk

2 answers 2

In nuget there is a DotNetZip library, it allows you to archive to a stream without first saving to disk. True, this is fraught with OutOfMemoryException. Anyway, when I tried to archive to a stream, this exception fell out regularly, so I had to return to the idea of ​​saving to disk.

  • Apparently, you have to pre-save the data to a file on disk. And I would like to avoid a third-party library (they got rid of it not so long ago). - Vlad
 using System.IO.Compression; private void Compress() { ZipFile.CreateFromDirectory(DirectorySrc , DirectoryDist); }