【问题标题】:C# Listing ZIP file contents - using ZipArchive ClassC# 列出 ZIP 文件内容 - 使用 ZipArchive 类
【发布时间】:2013-04-17 20:32:21
【问题描述】:

我想使用“ZipArchive”类获取 ZIP 文件中的内容列表。

我使用的是 MSDN 中的示例:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Compression;
using System.IO.Compression.dll;

namespace Zip_Extractor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (FileStream zipToOpen = new FileStream(@"c:\users\exampleuser\release.zip", FileMode.Open))
            {
                using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
                {
                    ZipArchiveEntry readmeEntry = archive.CreateEntry("Readme.txt");
                    using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
                    {
                            writer.WriteLine("Information about this package.");
                            writer.WriteLine("========================");
                    }
                }
            }
        }
    }
}

但是,我得到了错误

'找不到'ZipArchive 的命名空间'。

我可以确认应用程序的“目标框架”是 .NET Framework 4.5。

谁能指出我正确的方向?

【问题讨论】:

  • 您是否添加了System.IO.Compression.dll 作为参考?
  • 添加这个会给我错误“命名空间 System.IO.Compression 中不存在类型或命名空间名称 'dll'”。我认为这可能是 .NET 版本的问题。

标签: c# zip .net


【解决方案1】:

添加对System.IO.CompressionSystem.IO.Compression.FileSystem 的引用。

查看 David Anderson 的博客:http://www.danderson.me/dotnet/zipfile-class-system-io-compression-filesystem/

希望对您有所帮助!

【讨论】:

  • 我已经添加了这些参考。但是我收到错误,命名空间 System.IO.Compression 中不存在类型或命名空间名称“FileSystem”。有什么想法吗?
  • 当我在应用程序选项卡上选择项目>{项目名称}属性时 - “目标框架”是 .NET Framework 4.5。我正在使用 Visual Studio Pro 2012。
  • 尝试为应用设置完全信任。
  • 对“这是一个完全信任的应用程序”不满意。 - 我仍然收到错误 1 ​​名称空间“System.IO.Compression”中不存在类型或名称空间名称“dll”(您是否缺少程序集引用?)
  • 您可以尝试使用 DotNetZip。它具有与 ZipFile 类相同的功能。
【解决方案2】:

右键引用,添加对框架程序集 System.IO.Compression 和 System.IO.Compression.FileSystem 的引用。包括使用 System.IO.Compression;并确保您使用的是 .NET Framework 4.5。

【讨论】:

    【解决方案3】:

    我迟到了,但它可能对某人有所帮助......

    添加对其他答案中提到的文件的引用:

    • System.IO.压缩
    • System.IO.Compression.FileSystem

    添加后可能无法正常工作,因此进入它们的属性并将每个“复制本地”设置标记为“真”。

    为我做了诀窍。 :)

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,并在 Assemblies -> Framework: 中寻找这些命名空间
      只需添加命名空间:System.IO.Compression & System.IO.Compression.FileSystem

      这是使理解/事情更容易的屏幕截图:

      【讨论】:

        猜你喜欢
        • 2013-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-07
        • 2018-07-13
        • 1970-01-01
        相关资源
        最近更新 更多