将Zip文件转换为byte [],将byte []转换为zip文件

Sum*_*jee 1 c# file

我将zip文件转换为byte []并将其写入文本文件。

 int BufferSize=65536;
     private void button1_Click(object sender, EventArgs e)
        {
            DialogResult re = openFileDialog1.ShowDialog();
            if (re == DialogResult.OK)
            {
                string fileName = openFileDialog1.FileName;
                try
                {
                    byte[] bytes = File.ReadAllBytes(fileName);
                    File.WriteAllBytes(@"F:\Info.txt", bytes);
                }
                catch (Exception) { }
            }
        }   
Run Code Online (Sandbox Code Playgroud)

然后,我尝试将那些字节转换为zip文件。但是我做不到。

我的代码在这里:

private void button2_Click(object sender, EventArgs e)
        {

            DialogResult re = openFileDialog1.ShowDialog();
            if (re == DialogResult.OK)
            {
                string fileName = openFileDialog1.FileName;
                try
                {
                    byte[] bytes = File.ReadAllBytes(fileName);
                    using (var mstrim = new MemoryStream(bytes))
                    {
                        using (var inStream = new GZipStream(mstrim, CompressionMode.Compress))
                        {
                            using (var outStream = File.Create("Tax.Zip"))
                            {
                                var buffer = new byte[BufferSize];
                                int readBytes;
                                while ((readBytes = inStream.Read(buffer, 0, BufferSize)) != 0)
                                {
                                    outStream.Write(buffer, 0, readBytes);
                                }
                            }
                        }
                    }
                }
                catch (Exception) { }
            }
        }
Run Code Online (Sandbox Code Playgroud)

错误:文件模式无效。

Sum*_*jee 5

尝试一下。

            byte[] data = File.ReadAllBytes("D:\\z.7z");
            File.WriteAllBytes("D:\\t.txt", data); // Requires System.IO

            byte[] newdata = File.ReadAllBytes("D:\\t.txt");
            File.WriteAllBytes("D:\\a.7z", newdata); // Requires System.IO
Run Code Online (Sandbox Code Playgroud)