在c#中创建和下载zip文件

时间:2015-11-12 15:31:12

标签: c# .net

在我的c#app中,我尝试从用户提供的附件中创建一个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'    

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

由于错误显示Email静态类,因此您无法创建一个EventId实例。

System.IO.Compression.ZipFile类只公开静态方法来压缩目录,压缩你想要的特定文件System.IO.Compression.ZipArchive

答案 1 :(得分:0)

在MSDN上查看How to: Compress and Extract Files

确保您拥有.NET 4.5或更高版本。我假设您正在尝试使用System.IO.Compression。

答案 2 :(得分:0)

使用e.Graphics.DrawImage(bm,0, 0);,您可以使用GZipStream类来压缩和解压缩流。

如前所述here要使用ZipFile类,您必须在项目中添加对System.Io.Compression程序集的引用;否则,在尝试编译时,您将收到以下错误消息:System.IO.Compression.FileSystem

相关问题