COM preSS文件,dotnetzip,当打开已损坏 [英] Compress file with dotnetzip, and when open it is corrupted

查看:148
本文介绍了COM preSS文件,dotnetzip,当打开已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建从字节数组控制器的zip文件,我返回zip文件作为fileresult。当我下载ZIP文件并解压文件,它已损坏。我做的是这样的:

 字节[] = fileBytes数组
MemoryStream的FILESTREAM =新的MemoryStream(fileBytes);
MemoryStream的的OutputStream =新的MemoryStream();
fileStream.Seek(0,SeekOrigin.Begin);使用(ZipFile的压缩文件=新的ZipFile())
{
    zipFile.AddEntry(returnFileName,FILESTREAM);
    zipFile.Save(OutputStream的);
}outputStream.Position = 0;FileStreamResult fileResult =新FileStreamResult(OutputStream的,System.Net.Mime.MediaTypeNames.Application.Zip);
fileResult.FileDownloadName = returnFileName +.ZIP返回fileResult;


解决方案

您可能会创下的 DotNetZip 的开放的错误之一是不吉利的。有例如根据文件大小的问题(的https://dotnetzip.$c$cplex.com/workitem / 14087 )。

不幸的是,的 DotNetZip 的有一些关键问题,该项目似乎不再积极进行维护。更好的办法是使用SharpZipLib(如果你遵守其基于GPL的许可证),或 .NET的zlib端口。

如果你在.NET 4.5,你可以在 System.IO.Com pression 命名空间。下面的示例可以在 ZipArchive 类:

 使用系统;
使用System.IO;
使用System.IO.Com pression;命名空间ConsoleApplication
{
    类节目
    {
        静态无效的主要(字串[] args)
        {
            使用(VAR zipToOpen =
                新的FileStream(@C:\\ tmp目录\\ release.zip,FileMode.Open))
            {
                使用(VAR存档=
                     新的ZipArchive(zipToOpen,ZipArchiveMode.Update))
                {
                    VAR readmeEntry = archive.CreateEntry(的readme.txt);
                    使用(VAR作家=新的StreamWriter(readmeEntry.Open()))
                    {
                            writer.WriteLine(关于这个包的信息。);
                            writer.WriteLine(========================);
                    }
                }
            }
        }
    }
}

I create a zip file in a controller from a byte array and I return the zip file as a fileresult. When I download the zip File and extract the file, it is corrupt. I'm doing it this way:

byte[] fileBytes =array
MemoryStream fileStream = new MemoryStream(fileBytes);
MemoryStream outputStream = new MemoryStream();
fileStream.Seek(0, SeekOrigin.Begin);

using (ZipFile zipFile = new ZipFile())
{
    zipFile.AddEntry(returnFileName, fileStream);
    zipFile.Save(outputStream);
}

outputStream.Position = 0;

FileStreamResult fileResult = new FileStreamResult(outputStream, System.Net.Mime.MediaTypeNames.Application.Zip);
fileResult.FileDownloadName = returnFileName + ".zip";

return fileResult;

解决方案

You might be unlucky hitting one of the open bugs in DotNetZip. There is e.g. an issue depending on the file size (https://dotnetzip.codeplex.com/workitem/14087).

Unfortunately, DotNetZip has some critical issues and the project seems no longer be actively be maintained. Better alternatives would be to use SharpZipLib (if you comply with their GPL-based license), or one of the .NET ports of zlib.

If you are on .NET 4.5 you can use the built-in classes in the System.IO.Compression namespace. The following sample can be found in the documentation of the ZipArchive class:

using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var zipToOpen = 
                new FileStream(@"c:\tmp\release.zip", FileMode.Open))
            {
                using (var archive = 
                     new ZipArchive(zipToOpen, ZipArchiveMode.Update))
                {
                    var readmeEntry = archive.CreateEntry("Readme.txt");
                    using (var writer = new StreamWriter(readmeEntry.Open()))
                    {
                            writer.WriteLine("Information about this package.");
                            writer.WriteLine("========================");
                    }
                }
            }
        }
    }
}

这篇关于COM preSS文件,dotnetzip,当打开已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆