【问题标题】:C# unzip a file: Error with 'Thumbs.db'C# 解压缩文件:“Thumbs.db”出错
【发布时间】:2016-03-26 12:16:52
【问题描述】:

我编写了一个使用 SharpZipLib 解压缩文件 (.zip) 的程序...

以下代码:

public void UnZip(string zipFilePath, string extractionPath)
{
     FastZip fz = new FastZip();
     fz.ExtractZip(zipFilePath, extractionPath, null);
}

我得到以下异常:
附加信息:对路径 "C:\Program files (x86)\... Thumbs.db" 的访问被拒绝。
该程序以管理员权限启动,并且文件 "Thumbs.db" 在 .zip 存档中不存在。

谁知道得更多?
问候和感谢!

【问题讨论】:

  • Thumbs.db 是一个 Windows 内部文件,用于包含图像的文件夹的故事预览图片/微型图片。可以查看 zip 并验证该文件的存在吗?您可以创建一个没有此类文件的 zip 文件,然后尝试解压缩它吗?您的问题也可能只是您试图将其提取到您没有写入权限的C:\Program files (x86)\..。尝试解压到当前目录或桌面调试。
  • 感谢您的评论!我会在几分钟后尝试...但是我应该可以访问该路径,因为程序以管理员权限(app.manifest-data)开始

标签: c# unzip sharpziplib


【解决方案1】:

我会忽略“Thumbs.db”文件作为它的操作系统文件。

可能是这样的:

using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;

public void ExtractZipFile(string archiveFilenameIn, string password, string outFolder) {
    ZipFile zf = null;
    try {
        FileStream fs = File.OpenRead(archiveFilenameIn);
        zf = new ZipFile(fs);
        if (!String.IsNullOrEmpty(password)) {
            zf.Password = password;     // AES encrypted entries are handled automatically
        }
        foreach (ZipEntry zipEntry in zf) {
            if (!zipEntry.IsFile) {
                continue;           // Ignore directories
            }
            String entryFileName = zipEntry.Name;
            // to remove the folder from the entry:- entryFileName = Path.GetFileName(entryFileName);
            // Optionally match entrynames against a selection list here to skip as desired.
            // The unpacked length is available in the zipEntry.Size property.

            byte[] buffer = new byte[4096];     // 4K is optimum
            Stream zipStream = zf.GetInputStream(zipEntry);

            // Manipulate the output filename here as desired.
            String fullZipToPath = Path.Combine(outFolder, entryFileName);
            string directoryName = Path.GetDirectoryName(fullZipToPath);
            if (directoryName.Length > 0)
                Directory.CreateDirectory(directoryName);

            // Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size
            // of the file, but does not waste memory.
            // The "using" will close the stream even if an exception occurs.
            using (FileStream streamWriter = File.Create(fullZipToPath)) {
                StreamUtils.Copy(zipStream, streamWriter, buffer);
            }
        }
    } finally {
        if (zf != null) {
            zf.IsStreamOwner = true; // Makes close also shut the underlying stream
            zf.Close(); // Ensure we release resources
        }
    }
}

【讨论】:

  • 这不起作用,我得到一个异常:“ICSharpCode.SharpZipLib.Zip.ZipFile”不包含“Read”和“SelectEntries”的定义
  • 我已经编辑了上面的代码。这是来自他们自己的文档。我现在无法测试这个,因为我是用手机输入的。我会在上面写着“String entryFileName = zipEntry.Name;”的行之后大致寻找那个文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-05
  • 1970-01-01
相关资源
最近更新 更多