Jump to content
Tuts 4 You
Sign in to follow this  
skypeaful

Stuck in Compress DeflateStream

Recommended Posts

skypeaful

Can anybody tell me how to convert LinqPad.Resource.dll to .bin?
I try to write tool encrypt & decrypt resource for LINQPad: 

http://www.linqpad.net/download.aspx

The tool work fine in decompress .bin file to .dll but it's stuck in compress dll to bin: Error occur -> Reading from the compression stream is not supported

Here is binary exe with resource .bin include

I hope someone can help me how to fix to improve my to for support compress .dll to .bin

The code is easy, I pasted it's here:

        private void button1_Click(object sender, EventArgs e) //decompress .bin to .dll
        {
            OpenFileDialog dialog = new OpenFileDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(dialog.FileName, FileMode.Open, FileAccess.ReadWrite);
                string path = dialog.FileName.Replace("bin", "dll");
                File.WriteAllBytes(path, Deflate(fs, false).ToArray());
                fs.Close();
                MessageBox.Show("Bin Decompressed!");
                this.button1.Enabled = false;
                this.button2.Enabled = true;
            }
        }

        private void button2_Click(object sender, EventArgs e) //compress dll to bin
        {
            OpenFileDialog dialog = new OpenFileDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(dialog.FileName, FileMode.Open, FileAccess.ReadWrite);
                string path = dialog.FileName.Replace("dll", "bin");
                File.WriteAllBytes(path, Deflate(fs, true).ToArray()); //fail when compress which I can't fix
                fs.Close();
                MessageBox.Show("Bin Compressed!");
                this.button1.Enabled = true;
                this.button2.Enabled = false;
            }
        }

        public static MemoryStream Deflate(Stream src, bool compress)
        {
            MemoryStream ms = new MemoryStream();
            Deflate(src, ms, compress);
            ms.Seek(0, SeekOrigin.Begin);
            return ms;
        }

        public static void Deflate(Stream src, Stream dst, bool compress)
        {
            DeflateStream ds = new DeflateStream(src, compress ? CompressionMode.Compress : CompressionMode.Decompress);
            Copy(ds, dst);
        }

        public static void Copy(Stream src, Stream dst)
        {
            byte[] buffer = new byte[65536];
            int read = 0;
            do
            {
                read = src.Read(buffer, 0, buffer.Length);
                dst.Write(buffer, 0, read);
            }
            while (read == buffer.Length);
        }

 

Edited by skypeaful (see edit history)

Share this post


Link to post
kao
public static void Deflate(Stream src, Stream dst, bool compress)
{
   DeflateStream ds = new DeflateStream(src, compress ? CompressionMode.Compress : CompressionMode.Decompress);
   Copy(ds, dst);
}

Copy-pasting code from github causes lots of problems..  :) Which stream is source and which is destination when compressing data?

Share this post


Link to post
skypeaful

Hi kao, I also rewrite 2 compress code

                 string path = dialog.FileName.Replace("dll", "bin");+ ".dll"
                File.WriteAllBytes(path, Compress1(dialog.FileName)); // or Compress2

      private static byte[] Compress1(String fileName)
        {
            using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            using (var compressStream = new MemoryStream())
            using (var deflateStream = new DeflateStream(compressStream, CompressionMode.Compress))
            {
                byte[] array = new byte[fs.Length];
                input.Read(array, 0, array.Length);
                deflateStream.Write(array, 0, array.Length);
                return compressStream.ToArray();
            }
        }

        private static byte[] Compress2(string fileName)
        {
            using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            using (var compressStream = new MemoryStream())
            using (var deflateStream = new DeflateStream(compressStream, CompressionMode.Compress))
            {
                fs.CopyTo(deflateStream);
                return compressStream.ToArray();
            }
        }

Compress1 and Compress2 have different result (so strange) and both of 2 encrypted .bin file is crash when decrypt.

Can you give some advice to fix?

Share this post


Link to post
skypeaful

Thank kao, I understand now.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
×
×
  • Create New...