使用zipinputstream解压缩文件 [英] Unzipping files using zipinputstream

查看:366
本文介绍了使用zipinputstream解压缩文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Team,



以下是解压缩zip文件夹的代码。我得到算术溢出异常,是的我理解这是因为字节最多可以处理255个字节。

Hi Team,

Below is the code for decompressing the zip folder . I get arithmetic overflow exceptions, Yes I understand that is because byte can handle maximum of 255 byte.

public static void DecompressArchive(StringBuilder newPath, ZipInputStream zipIn)
        {
            ZipEntry entry;
            while ((entry = zipIn.GetNextEntry()) != null)
            {
                if (entry.Name.EndsWith("/"))
                {
                    Directory.CreateDirectory(String.Format("{0}{1}", newPath, entry.Name.Replace(@"/", @"\")));
                }
                else
                {
                    FileStream streamWriter = File.Create(String.Format("{0}{1}", newPath, entry.Name.Replace(@"/", @"\")));
                    long size = entry.Size;
                    Byte[] data = new Byte[size];
                    while (true)
                    {
                        size = zipIn.Read(data, 0, data.Length);
                        if (size > 0) streamWriter.Write(data, 0, (int)size);
                        else break;
                    }
                    streamWriter.Close();
                }





我理解Byte数组不能超过255bytes。当我的尺寸大于255时,如何处理这种情况。



我尝试了什么:



尝试将字节转换为int ..但是,streamWriter.Write()方法接受数组的字节的第一个参数



I understand Byte array cannot take more than 255bytes. How do I handle this situation when my size is >255.

What I have tried:

Tried Converting Byte to int.. However the streamWriter.Write() method takes first parameters of byte of array

推荐答案





试试这段代码,这将解决您的问题。我测试了这个以解压缩900MB文件。



Hi,

Try this code, this will solve your problem. I have tested this to unzip 900MB file.

protected void btnUnzip_Click(object sender, EventArgs e)
        {
            StringBuilder newPath = new StringBuilder();
            newPath.Append(@"D:\Test\");
            string zipPathAndFile = @"F:\EnglishBook.zip";
            ZipInputStream zipIn = new ZipInputStream(File.OpenRead(zipPathAndFile));
            DecompressArchive(newPath, zipIn);
        }

        public static void DecompressArchive(StringBuilder newPath, ZipInputStream zipIn)
        {
            ZipEntry entry;
            while ((entry = zipIn.GetNextEntry()) != null)
            {
                if (entry.Name.EndsWith("/"))
                {
                    Directory.CreateDirectory(String.Format("{0}{1}", newPath, entry.Name.Replace(@"/", @"\")));
                }
                else
                {
                    FileStream streamWriter = File.Create(String.Format("{0}{1}", newPath, entry.Name.Replace(@"/", @"\")));
                    //long size = entry.Size;
                    int size = 1024 * 10;
                    Byte[] data = new Byte[size];
                    while (true)
                    {
                        size = zipIn.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            streamWriter.Write(data, 0, (int)size);
                        }
                        else break;
                    }
                    streamWriter.Close();
                }
            }
        }





注意:唯一的,我改变的是块的大小。





Note: Only thing, I have changed is size of chunk.

//long size = entry.Size;
int size = 1024 * 10;





谢谢,



Thanks,


这篇关于使用zipinputstream解压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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