如何解压缩在C#中,操作.bz2文件?(How to decompress .bz2 file in

2019-09-23 17:22发布

我开发WPF应用程序。 我使用sharpziplib压缩和解压缩文件。 我很容易地使用下面的代码解压缩.zip文件

public static void UnZip(string SrcFile, string DstFile, string safeFileName, int bufferSize)
        {
            //ICSharpCode.SharpZipLib.Zip.UseZip64.Off;

            FileStream fileStreamIn = new FileStream(SrcFile, FileMode.Open, FileAccess.Read);
            ZipInputStream zipInStream = new ZipInputStream(fileStreamIn);


            string rootDirectory = string.Empty;
            if (safeFileName.Contains(".zip"))
            {
                rootDirectory = safeFileName.Replace(".zip", string.Empty);
            }
            else
            {
                rootDirectory = safeFileName;
            }

            Directory.CreateDirectory(App.ApplicationPath + rootDirectory);

            while (true)
            {
                ZipEntry entry = zipInStream.GetNextEntry();

                if (entry == null)
                    break;

                if (entry.Name.Contains("/"))
                {
                    string[] folders = entry.Name.Split('/');

                    string lastElement = folders[folders.Length - 1];
                    var folderList = new List<string>(folders);
                    folderList.RemoveAt(folders.Length - 1);
                    folders = folderList.ToArray();

                    string folderPath = "";
                    foreach (string str in folders)
                    {
                        folderPath = folderPath + "/" + str;
                        if (!Directory.Exists(App.ApplicationPath + rootDirectory + "/" + folderPath))
                        {
                            Directory.CreateDirectory(App.ApplicationPath + rootDirectory + "/" + folderPath);
                        }
                    }

                    if (!string.IsNullOrEmpty(lastElement))
                    {
                        folderPath = folderPath + "/" + lastElement;
                        WriteToFile(DstFile + rootDirectory + @"\" + folderPath, bufferSize, zipInStream, rootDirectory, entry);
                    }

                }
                else
                {
                    WriteToFile(DstFile + rootDirectory + @"\" + entry.Name, bufferSize, zipInStream, rootDirectory, entry);
                }
            }

            zipInStream.Close();           
            fileStreamIn.Close();
        }

        private static void WriteToFile(string DstFile, int bufferSize, ZipInputStream zipInStream, string rootDirectory, ZipEntry entry)
        {
            FileStream fileStreamOut = new FileStream(DstFile, FileMode.OpenOrCreate, FileAccess.Write);
            int size;
            byte[] buffer = new byte[bufferSize];

            do
            {
                size = zipInStream.Read(buffer, 0, buffer.Length);
                fileStreamOut.Write(buffer, 0, size);
            } while (size > 0);

            fileStreamOut.Close();
        }

但相同的代码不与操作.bz2文件的工作。 它以线给错误

ZipEntry entry = zipInStream.GetNextEntry();

错误是 - 错误的本地头标识:0x26594131。 我应该如何解压缩操作.bz2文件? 能否请您给我提供任何代码或链接,通过它我可以解决上述问题?

Answer 1:

当您使用ZipInputStream.zip文件,你应该使用BZip2InputStream.bz2文件( GZipInputStream.gz文件等)。



Answer 2:

不像邮编(和RAR和焦油),BZ2和gzip只是字节流的压缩机。 他们没有像上述的容器格式的概念,因此它为什么上失败GetNextEntry 。 (换句话说,BZ2和gzip将只具有至多1个条目)。



文章来源: How to decompress .bz2 file in C#?