Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用ICSharpCode.SharpZipLib将文件夹添加到zip存档_C#_Zip_Sharpziplib_Zipoutputstream - Fatal编程技术网

C# 如何使用ICSharpCode.SharpZipLib将文件夹添加到zip存档

C# 如何使用ICSharpCode.SharpZipLib将文件夹添加到zip存档,c#,zip,sharpziplib,zipoutputstream,C#,Zip,Sharpziplib,Zipoutputstream,我必须在一个zip文件中创建两个文件夹,我使用ICSharpCode.SharZipLib.zip以编程方式创建该文件。我想: private void AddToZipStream(byte[] inputStream, ZipOutputStream zipStream, string fileName, string fileExtension) { var courseName = RemoveSpecialCharacters(fileName);

我必须在一个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存档中的目录?

我想:

  • 您只需执行
    newzipentry(“Folder1/Archive.txt”)
    新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的文件一样,并在末尾加上正斜杠。它起作用了。我已经测试过了。

我们项目的最佳解决方案是切换到更好的方式


这些方法使用起来更直接

谢谢您的评论。我没有正确地问这个问题。我真正想做的是创建一个文件夹+文件的组合,我给出的答案是可行的。是的。我只是添加了我的评论,因为我在谷歌上搜索添加空文件夹的答案。现在人们会发现这两个答案。就像许多公司的情况一样,我没有选择使用图书馆。这个图书馆用了好几年了。不过我已经不在这家公司工作了:)所以你敢打赌我会使用dotnetzip,特别是semverd。