Question

I am developing wpf application. I am using sharpziplib to compress and decompress files. I am easily decompress the .zip files using following code

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();
        }

But the same code is not working with .bz2 files. It is giving error at line

ZipEntry entry = zipInStream.GetNextEntry();

The error is - Wrong Local header signature: 0x26594131. How should I decompress the .bz2 file ? Can you please provide me any code or link through which I can resolve the above issue ?

Was it helpful?

Solution

While you use a ZipInputStream for .zip files, you should use a BZip2InputStream for .bz2 files (and GZipInputStream for .gz files etc.).

OTHER TIPS

Unlike Zip (and RAR and tar), bz2 and gzip are just byte stream compressors. They have no concept of a container format like the aforementioned, and hence why it fails on GetNextEntry. (In other words, bz2 and gzip will only have 1 entry at most).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top