You are viewing our Forum Archives. To view or take place in current topics click here.
C# need help with zlib.
Posted:

C# need help with zlib.Posted:

PHP_Pro
  • Ladder Climber
Status: Offline
Joined: Oct 12, 201112Year Member
Posts: 349
Reputation Power: 20
Status: Offline
Joined: Oct 12, 201112Year Member
Posts: 349
Reputation Power: 20
I am having issues with zlib. i am reading a file into 4096 byte blocks, compressing the blocks, then writing them to the new file.
That part works fine, as in , no exceptions are thrown.
Here is the code that i use to compress the files.

public static void CreateContainer(List<string> Files, string OutPath, FileMode filemode)
        {
            foreach(string s in Files)
            {
                string path = s;
                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    using (FileStream fout = new FileStream(OutPath, filemode, FileAccess.ReadWrite, FileShare.ReadWrite))
                    {
                        int Blocksize = 4096;
                        long progress = 0;
                        int BCB = 0;
                        byte[] buffer = new byte[Blocksize];
                        while (progress < fs.Length)
                        {
                            fout.Position = fout.Length;
                            if ((fs.Length - progress) >= 4096)
                            {
                                BCB = fs.Read(buffer, 0, 4096);
                                byte[] compressed = ZlibStream.CompressBuffer(buffer);
                                fout.Write(compressed, 0, compressed.Length);
                            }
                            else
                            {
                                byte[] Remaining = new byte[(int)(fs.Length - progress)];
                                BCB = fs.Read(Remaining, 0, Remaining.Length);
                                byte[] compressed =ZlibStream.CompressBuffer(Remaining);
                                fout.Write(compressed, 0, compressed.Length);
                            }
                            progress += (long)BCB;
                        }
                    }
                }
            }
        }

That is not where the exception is thrown, it is when i attempt to read && decompress the compressed bytes.
Here is the code that i use for that -

 public static void ExtractEntry(string ContainerPath, string Entryname, string Outpath)
        {
            using (FileStream fs = new FileStream(ContainerPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                using (FileStream fout = new FileStream(Outpath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    List<FileEntry> entrys = ParseContainer(ContainerPath);
                    for (int i = 0; i < entrys.Count; i++)
                    {
                        if (entrys[i].Filename == Entryname)
                        {
                             int Blocksize = 4096;
                             long progress = 0;
                             int BCB = 0;
                             byte[] buffer = new byte[Blocksize];
                             fs.Position = entrys[i].StartPosition;
                             while (progress < entrys[i].Length)
                             {
                                 if ((entrys[i].Length - progress) >= 4096)
                                 {
                                     BCB = fs.Read(buffer, 0, 4096);
                                     byte[] decomp = ZlibStream.UncompressBuffer(buffer);
                                     fout.Write(decomp, 0, decomp.Length);
                                 }
                                 else
                                 {
                                     byte[] Remaining = new byte[(int)(entrys[i].Length - progress)];
                                     BCB = fs.Read(Remaining, 0, Remaining.Length);
                                     byte[] decomp=ZlibStream.UncompressBuffer(Remaining);
                                     fout.Write(decomp, 0, decomp.Length);
                                 }
                                 progress += (long)BCB;
                             }
                         }
                    }
                }
            }
        }

It writes the first 4 kb of data (to the output file), but after that, zlib throws an exception.
"Bad state (unknown compression method)"
Does anyone have a solution?
i cannot read the entire file into memory, it must be in small chunks.

Any help would be appreciated!
#2. Posted:
RDCA
  • TTG Contender
Status: Offline
Joined: Jul 12, 201013Year Member
Posts: 3,612
Reputation Power: 173
Status: Offline
Joined: Jul 12, 201013Year Member
Posts: 3,612
Reputation Power: 173
Ugh, I am too lazy to download zlib, but I have an idea. Before decompressing the bytes, add them together and decompress them, and then right them to the file.
#3. Posted:
PHP_Pro
  • Ladder Climber
Status: Offline
Joined: Oct 12, 201112Year Member
Posts: 349
Reputation Power: 20
Status: Offline
Joined: Oct 12, 201112Year Member
Posts: 349
Reputation Power: 20
RDCA wrote Ugh, I am too lazy to download zlib, but I have an idea. Before decompressing the bytes, add them together and decompress them, and then right them to the file.

Odd, when i tried that( Storing the compressed bytes in a MemoryStream, then decompressing), no exception was thrown , but it still did not write the whole file. it still writes only the first 4096 bytes.
Their must be something wrong with compressing a file in chunks?
Thanks for the suggestion though.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.