Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Stuck in Compress DeflateStream

Featured Replies

Posted

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

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?

  • Author

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?

  • Author

Thank kao, I understand now.

Create an account or sign in to comment

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.