相关疑难解决方法(0)

使用System.IO.Compression在内存中创建ZIP存档

我正在尝试使用MemoryStream如下的简单演示文本文件创建ZIP存档:

using (var memoryStream = new MemoryStream())
using (var archive = new ZipArchive(memoryStream , ZipArchiveMode.Create))
{
    var demoFile = archive.CreateEntry("foo.txt");

    using (var entryStream = demoFile.Open())
    using (var streamWriter = new StreamWriter(entryStream))
    {
        streamWriter.Write("Bar!");
    }

    using (var fileStream = new FileStream(@"C:\Temp\test.zip", FileMode.Create))
    {
        stream.CopyTo(fileStream);
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我运行此代码,则会创建存档文件本身,但不会创建foo.txt.

但是,如果我MemoryStream直接替换文件流,则会正确创建存档:

using (var fileStream = new FileStream(@"C:\Temp\test.zip", FileMode.Create))
using (var archive = new ZipArchive(fileStream, FileMode.Create))
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

是否可以使用a MemoryStream创建ZIP存档而不用FileStream

.net c# compression zip ziparchive

163
推荐指数
5
解决办法
15万
查看次数

如何从C#内存中的文件创建ZipArchive?

是否有可能从内存中的文件创建ZipArchive(而不是实际上在磁盘上).

以下是用例:IEnumerable<HttpPostedFileBase>变量中接收多个文件.我想用所有这些文件压缩在一起ZipArchive.问题是ZipArchive只允许CreateEntryFromFile,它需要文件的路径,因为我只有内存中的文件.

问题: 有没有办法使用'stream'来创建'entry' ZipArchive,这样我就可以直接在zip中放入文件的内容?

我不想先保存文件,创建zip(从保存文件的路径),然后删除单个文件.

在这里,attachmentFilesIEnumerable<HttpPostedFileBase>

using (var ms = new MemoryStream())
{
    using (var zipArchive = new ZipArchive(ms, ZipArchiveMode.Create, true))
    {
        foreach (var attachment in attachmentFiles)
        {
            zipArchive.CreateEntryFromFile(Path.GetFullPath(attachment.FileName), Path.GetFileName(attachment.FileName),
                                CompressionLevel.Fastest);
        }
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

.net c# zip zipfile

15
推荐指数
1
解决办法
2万
查看次数

我应该如何将ZipArchive与内存流一起使用?

我的问题是,一旦ZipArchive处理完毕,它就会自动关闭并处理掉MemoryStream.如果我在处理ZipArchive信息之前看一下流不完整的拉链.

using (var compressStream = new MemoryStream())
{
    using (var zipArchive = new ZipArchive(compressStream, ZipArchiveMode.Create))
    {
        // Adding a couple of entries
        string navStackInfo = Navigation.NavState.CurrentStackInfoLines();
        var navStackEntry = zipArchive.CreateEntry("NavStack.txt", CompressionLevel.NoCompression);
        using (StreamWriter writer = new StreamWriter(navStackEntry.Open()))
        {
             writer.Write(navStackInfo);
        }
        var debugInfoEntry = zipArchive.CreateEntry("CallStack.txt", CompressionLevel.Optimal);
        using (StreamWriter writer = new StreamWriter(debugInfoEntry.Open()))
        {
            // debugInfo.Details is a string too
            writer.Write(debugInfo.Details);
        }
        // ...
        // compressStream here is not well formed
    }
    // compressStream here is …
Run Code Online (Sandbox Code Playgroud)

c# memorystream ziparchive

9
推荐指数
1
解决办法
1万
查看次数

如何在内存中创建一个zip文件,从文件字节开始?

我尝试使用c#在内存中创建一个zip文件,但结果是一个zip文件,里面有损坏的文件.所有zip文件都在数据库中.我存储字节.文件是PDF.我的代码如下

//[extract bytes and file name for every file]

using (var zipArchiveMemoryStream = new MemoryStream())
{
    using (var zipArchive = new ZipArchive(zipArchiveMemoryStream,
                                           ZipArchiveMode.Create, true))
    {
        foreach (var file in fileData)
        {
            var zipEntry = zipArchive.CreateEntry(file.FileName);
            using (var entryStream = zipEntry.Open())
            {
                using (var tmpMemory = new MemoryStream(file.Bytes))
                {
                    tmpMemory.CopyTo(entryStream);
                };

            }
        }
    }
    zipArchiveMemoryStream.Position = 0;
    result = zipArchiveMemoryStream.GetBuffer();

//[download zip file]
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的支持

TONIGNO解决方案: 问题是我使用GetBuffer(); 而是ToArray();

using (var zipArchiveMemoryStream = new MemoryStream())
        {
            using (var zipArchive = new ZipArchive(zipArchiveMemoryStream, ZipArchiveMode.Create, …
Run Code Online (Sandbox Code Playgroud)

c# compression zip

5
推荐指数
1
解决办法
4464
查看次数

从字节(在内存中使用任意编码的文本)在内存中创建zip文件

我正在开发的应用程序需要将xml文件压缩为zip文件,并通过http请求将其发送到Web服务。因为我不需要保留zip文件,所以我只是在内存中执行压缩。Web服务拒绝了我的请求,因为zip文件显然格式错误。

我知道此问题中有一个解决方案可以很好地工作,但它使用了StreamWriter。该解决方案的我的问题是,StreamWriter需要编码或假设UTF-8,并且我不需要知道xml文件的编码。我只需要从这些文件中读取字节,然后将它们存储在zip文件中,无论它们使用哪种编码即可。

因此,很明显,这个问题与编码无关,因为我不需要将字节转换为文本或相反。我只需要压缩一个byte[]

我正在使用下一个代码来测试zip文件的格式是否错误:

static void Main(string[] args)
{
    Encoding encoding = Encoding.GetEncoding("ISO-8859-1");

    string xmlDeclaration = "<?xml version=\"1.0\" encoding=\"" + encoding.WebName.ToUpperInvariant() + "\"?>";
    string xmlBody = "<Test>ª!\"·$%/()=?¿\\|@#~€¬'¡º</Test>";
    string xmlContent = xmlDeclaration + xmlBody;
    byte[] bytes = encoding.GetBytes(xmlContent);
    string fileName = "test.xml";
    string zipPath = @"C:\Users\dgarcia\test.zip";

    Test(bytes, fileName, zipPath);
}

static void Test(byte[] bytes, string fileName, string zipPath)
{
    byte[] zipBytes;

    using (var memoryStream = new MemoryStream())
    using (var zipArchive = …
Run Code Online (Sandbox Code Playgroud)

c# compression zip

2
推荐指数
1
解决办法
950
查看次数

标签 统计

c# ×5

zip ×4

compression ×3

.net ×2

ziparchive ×2

memorystream ×1

zipfile ×1