How do you add a folder to a zip archive with ICSharpCode.SharpZipLib

Saturn K picture Saturn K · Aug 21, 2013 · Viewed 8.1k times · Source

I have to create two folders inside of a zip file that I create programmatically using ICSharpCode.SharZipLib.Zip. I want to:

    private void AddToZipStream(byte[] inputStream, ZipOutputStream zipStream, string fileName, string fileExtension)
    {
        var courseName = RemoveSpecialCharacters(fileName);

        var m_Bytes = inputStream;
        if ((m_Bytes != null) && (zipStream != null))
        {
            var newEntry = new ZipEntry(ZipEntry.CleanName(string.Concat(courseName, fileExtension)));
            newEntry.DateTime = DateTime.Now;
            newEntry.Size = m_Bytes.Length;

            zipStream.PutNextEntry(newEntry);
            zipStream.Write(m_Bytes, 0, m_Bytes.Length);
            zipStream.CloseEntry();
            zipStream.UseZip64 = UseZip64.Off;
        }
    }

How do I create a directory using ZipEntry and how do then add files to the directory located inside of the Zip archive?

Answer

Saturn K picture Saturn K · Aug 21, 2013

I figured it out:

  • You can simply do new ZipEntry("Folder1/Archive.txt"); and new ZipEntry("Folder2/Archive2.txt");