sharpziplib FastZip Extract RAR Ошибка не удается найти центральный каталог c# - PullRequest
0 голосов
/ 04 апреля 2020

Я хочу извлечь RAR-файл с помощью FastZip, вот мой код:

 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 без ошибок, так как извлечь файл RAR с помощью sharpziplib с FastZip или без FastZip? Примечание: я не хочу использовать SharpCompress, потому что я не поддерживаю пароль. Любой способ извлечь файл RAR, используя sharpziplib? Спасибо за помощь

1 Ответ

0 голосов
/ 04 апреля 2020

вот как извлечь файл 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);
                            }
                        }
                    }

Если честно, эта работа только с файлом rar, созданным с sharpziplib, не открывает файл rar, созданный с помощью winrar

...