C#解压缩文件:'Thumbs.db'

时间:2016-03-26 12:16:53

标签: c# unzip sharpziplib

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

以下代码:

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

我得到以下例外:
附加信息:拒绝访问路径"C:\Program files (x86)\... Thumbs.db" 该程序以管理员权限开头,而.zip存档中不存在文件"Thumbs.db"

谁进一步了解?
问候和谢谢!

1 个答案:

答案 0 :(得分:0)

我会忽略" 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
        }
    }
}