SharpZipLib与WinZip版本文件有关的问题

问题描述

嗨,我正在尝试使用SharpZipLib v1.2.0库解压缩zip文件,但我无法使其正常工作。 我尝试解压缩的文件是使用WinZip 9.0制作的,并且具有密码保护功能

我使用此代码尝试解压缩文件,但出现以下消息时抛出异常:

ICSharpCode.SharpZipLib.Zip.ZipException:'该库不支持提取该条目所需的zip版本'

    public static Dictionary<string,MemoryStream> ExtractZipContent(MemoryStream m,string password)
    {
        Dictionary<string,MemoryStream> files = new Dictionary<string,MemoryStream>();
        ZipFile file = null;
        try
        {
            file = new ZipFile(m);
            file.UseZip64 = UseZip64.On;
            if (!String.IsNullOrEmpty(password))
            {
                // AES encrypted entries are handled automatically
                file.Password = password;
            }

            foreach (ZipEntry zipEntry in file)
            {
                if (!zipEntry.IsFile)
                {
                    // Ignore directories
                    continue;
                }

                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.

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

                // Manipulate the output filename here as desired.
                //String fullZipToPath = Path.Combine(OutputFolder,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))
                //{

                MemoryStream s = new MemoryStream();
                StreamUtils.copy(zipStream,s,buffer);
                files.Add(entryFileName,s);
            }
        }
        finally
        {
            if (file != null)
            {
                file.IsstreamOwner = true; // Makes close also shut the underlying stream
                file.Close(); // Ensure we release resources
            }
        }
        return files;
    }

即使我尝试使用其他代码但没有结果,但现在却给我带来了另一个错误

ICSharpCode.SharpZipLib.Zip.ZipException:'无效的密码'

但是密码正确,我可以使用7Zip或WinZip10解压缩文件

 public static Dictionary<string,MemoryStream> UnZip(MemoryStream stream,MemoryStream>();
        using (ZipInputStream zip = new ZipInputStream(stream))
        {
            zip.Password = password;
            //file.UseZip64 = UseZip64.On;

            //file.CommitUpdate();
            ZipEntry zipEntry;
            if ((zipEntry = zip.GetNextEntry()) != null)
            {
                string directoryName = Path.GetDirectoryName(zipEntry.Name);
                string fileName = Path.GetFileName(zipEntry.Name);
                Console.WriteLine($"zipEntry directoryName={directoryName} fileName={fileName}");
                // create directory
                if (directoryName.Length > 0)
                {
                    Directory.CreateDirectory(directoryName);
                }
                if (fileName != String.Empty)
                {
                    MemoryStream streamWriter = new MemoryStream();
                    int size = 2048;
                    byte[] buffer = new byte[2048];
                    while (true)
                    {
                        size = zip.Read(buffer,buffer.Length);
                        if (size > 0)
                        {
                            streamWriter.Write(buffer,size);
                        }
                        else
                        {
                            break;
                        }
                    }
                    files.Add(fileName,streamWriter);
                }
            }
        }
        return files;
    }

该zip文件是由三分之一创建的,因此我无法更改版本或创建方式

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)