Click here to Skip to main content
15,891,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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();
                }


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
Posted
Updated 4-Aug-16 9:09am
v2
Comments
Mehdi Gholam 4-Aug-16 2:03am    
Where is the code from?
ShaHam11 4-Aug-16 2:18am    
I got it from google from a blog
Parveen Siwach 4-Aug-16 2:24am    
I tried this and is working fine. Can you share the Zip file that you want to unzip?

protected void btnUnzip_Click(object sender, EventArgs e)
{
StringBuilder newPath = new StringBuilder();
newPath.Append(@"D:\Test\");
string zipPathAndFile = @"D:\Data\User_Data.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;
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();
}
}
}
ShaHam11 4-Aug-16 2:34am    
it works if the file size is less have zipped folder which is about 800 MB and more

1 solution

Hi,

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

C#
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.

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


Thanks,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900