0

我想使用 FastZip 提取 RAR 文件,这是我的代码:

 FastZip fastZip = new FastZip();
                     fastZip.CreateEmptyDirectories = true;
 if (password != "")
                        {
                            fastZip.Password = password;
                        }

                        string fileFilter = null;

                        fastZip.ExtractZip(CompressedFilePathValue, OutputFolderPathValue, fileFilter);

但我总是得到错误:

 cannot find central directory

RAR 文件没问题,我用 WinRAR 打开它没有错误,那么如何使用带有 FastZip 或不使用 FastZip 的 sharpziplib 提取 RAR 文件?注意:我不想使用 SharpCompress,因为我不支持密码。有什么方法可以使用sharpziplib 提取RAR 文件?感谢帮助

4

1 回答 1

0

这是如何提取RAR文件,没有错误找不到中央目录:

   using (Stream fs = File.OpenRead(CompressedFilePathValue))
                    using (var zf = new ZipFile(fs))
                    {

                        if (!String.IsNullOrEmpty(password))
                        {
                            // AES encrypted entries are handled automatically
                            zf.Password = password;
                        }

                        foreach (ZipEntry zipEntry in zf)
                        {
                            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.

                            // Manipulate the output filename here as desired.
                            var fullZipToPath = Path.Combine(OutputFolderPathValue, entryFileName);
                            var directoryName = Path.GetDirectoryName(fullZipToPath);
                            if (directoryName.Length > 0)
                            {
                                Directory.CreateDirectory(directoryName);
                            }

                            // 4K is optimum
                            var buffer = new byte[4096];

                            // 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 (var zipStream = zf.GetInputStream(zipEntry))
                            using (Stream fsOutput = File.Create(fullZipToPath))
                            {
                                StreamUtils.Copy(zipStream, fsOutput, buffer);
                            }
                        }
                    }

老实说,这项工作仅适用于使用 sharpziplib 创建的 rar 文件,它不会打开使用 winrar 创建的 rar

于 2020-04-04T19:02:36.117 回答