如何在C#中解压缩.bz2文件?

时间:2012-10-08 13:43:19

标签: c# unzip sharpziplib

我正在开发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文件?能否请您提供我可以解决上述问题的任何代码或链接?

2 个答案:

答案 0 :(得分:3)

ZipInputStream文件使用.zip时,您应该BZip2InputStream使用.bz2个文件(GZipInputStream用于.gz个文件等)。

答案 1 :(得分:3)

与Zip(和RAR和tar)不同,bz2和gzip只是字节流压缩器。它们没有像前面提到的容器格式的概念,因此它在GetNextEntry上失败了。 (换句话说,bz2和gzip最多只有1个条目。)