将Zip文件转换为byte [],将byte []转换为zip文件 [英] Convert Zip File to byte[] and byte[] to zip file

查看:143
本文介绍了将Zip文件转换为byte [],将byte []转换为zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to convert zip file to byte[] and write it to a text file.

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) { }
    }
}   

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

Then I try to convert those byte to zip file. But I can't do it.

我的代码在这里:

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) { }
    }
}

错误:文件模式无效.

Error:File Mode not valid.

需要哪种文件模式,我该如何完成我描述的内容?

What File Mode is needed and how can I accomplish what I described?

推荐答案

只需尝试一下.

            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

这篇关于将Zip文件转换为byte [],将byte []转换为zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆