繁体   English   中英

在C#中创建并下载zip文件

[英]Create and download zip file in c#

在我的C#应用​​程序中,我试图根据从用户那里获得的附件创建一个zip文件,然后下载该zip文件。 首先,我尝试使用SharpZipLib,但是没有用。

我收到以下错误:

线程中止错误。

现在我正在尝试使用

System.IO.Compression;

但那里有些问题,因为我收到此错误:

The type or namespace name 'ZipFile' could not be found (are you missing a using directive or an assembly reference?)

我现在尝试添加dll参考

            using (ZipFile zip = new ZipFile())
            {
                zip.CompressionLevel = CompressionLevel.Fastest;

                zip.AddSelectedFiles(logoimage, "", false);
                zip.Save(HttpContext.Current.Response.OutputStream);
            }

我得到这个错误,

Cannot declare a variable of static type 'System.IO.Compression.ZipFile'    

有什么想法吗?

如错误所示, System.IO.Compression.ZipFile静态类,因此您无法创建一个new实例。

ZipFile类仅公开静态方法来压缩目录,以压缩要使用System.IO.Compression.ZipArchive的特定文件。

看一看如何:在MSDN上压缩和提取文件

确保您具有.NET 4.5或更高版本。 我假设您这样做,因为您尝试使用System.IO.Compression。

使用System.Io.Compression ,可以使用GZipStream类压缩和解压缩流。

如此处所述要使用ZipFile类,必须在项目中添加对System.IO.Compression.FileSystem程序集的引用。 否则,在尝试编译时会出现以下错误消息: The name 'ZipFile' does not exist in the current context

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM