簡體   English   中英

如何使用ICSharpCode.SharpZipLib將文件夾添加到zip存檔

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

我必須在zip文件中創建兩個文件夾,我使用ICSharpCode.SharZipLib.Zip編程方式創建。 我想要:

    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;
        }
    }

如何使用ZipEntry創建目錄,然后如何將文件添加到Zip存檔內的目錄?

我想到了:

  • 你可以簡單地做一個new ZipEntry("Folder1/Archive.txt"); new ZipEntry("Folder2/Archive2.txt");

上面的答案適用於幾種情況,但是當您想要將空文件夾添加到zip文件時,它將無法工作。

我篩選了SharpZipLib代碼,發現創建文件夾時唯一需要做的就是ZipEntry名稱上的尾部“/”正斜杠。

這是庫中的代碼:

public bool IsDirectory {
    get {
        int nameLength = name.Length;
        bool result =
            ((nameLength > 0) &&
            ((name[nameLength - 1] == '/') || (name[nameLength - 1] == '\\'))) ||
            HasDosAttributes(16)
            ;
        return result;
    }
}

因此,只需創建文件夾,就像它們是ZipEntry文件一樣,並在末尾添加正斜杠。 有用。 我測試過了。

我們項目的最佳解決方案是切換到更好的方式

https://dotnetzip.codeplex.com

https://github.com/haf/DotNetZip.Semverd

這些方法更直接使用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM