Click here to Skip to main content
15,919,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
zip a folder in asp.net c# without third party controll
Posted

Have a look at[^] the GZipStream class.
 
Share this answer
 
Add reference...

C#
using java.util;
using java.util.zip;
using java.io;

public string ZipFile(string savePath, string[] attachFiles)
    {
        string sFlag = "Error";
        try
        {
            FileOutputStream fileOutput = new FileOutputStream(savePath);
            ZipOutputStream zipOutput = new ZipOutputStream(fileOutput);
            FileInputStream fileInput = null;
            foreach (string file in attachFiles)
            {
                fileInput = new FileInputStream(file);
                ZipEntry zipEntry = new ZipEntry(Path.GetFileName(file));
                zipOutput.putNextEntry(zipEntry);
                sbyte[] buffer = new sbyte[1024];
                int len = 0;
                while ((len = fileInput.read(buffer)) >= 0)
                {
                    zipOutput.write(buffer, 0, len);
                }
            }
            zipOutput.closeEntry();
            fileInput.close();
            zipOutput.close();
            fileOutput.close();
            return sFlag = "Success";
        }
        catch (Exception)
        {
            return sFlag;
        }
    }
    private List<ZipEntry> GetZipFiles(ZipFile zipfill)
    {
        List<ZipEntry> listZip = new List<ZipEntry>();
        Enumeration zipEnum = zipfill.entries();
        while (zipEnum.hasMoreElements())
        {
            ZipEntry zipEntry = (ZipEntry)zipEnum.nextElement();
            listZip.Add(zipEntry);
        }
        return listZip;
    }
    public string UnZipFile(string zippedFile, string destPath)
    {
        string sFlag = "Error";
        try
        {
            ZipFile zipfile = new ZipFile(zippedFile);
            List<ZipEntry> zipFiles = GetZipFiles(zipfile);
            foreach (ZipEntry zipFile in zipFiles)
            {
                if (!zipFile.isDirectory())
                {
                    InputStream s = zipfile.getInputStream(zipFile);
                    try
                    {
                        Directory.CreateDirectory(destPath + "\\" + Path.GetDirectoryName(zipFile.getName()));
                        FileOutputStream dest = new FileOutputStream(Path.Combine(destPath + "\\" + Path.GetDirectoryName(zipFile.getName()), Path.GetFileName(zipFile.getName())));
                        try
                        {
                            int len = 0;
                            sbyte[] buffer = new sbyte[7168];
                            while ((len = s.read(buffer)) >= 0)
                            {
                                dest.write(buffer, 0, len);
                            }
                        }
                        finally
                        {
                            dest.close();
                        }
                    }
                    finally
                    {
                        s.close();
                    }
                }
                sFlag = "Success";
            }
            return sFlag;
        }
        catch (Exception)
        {
            return sFlag;
        }
    }
 
Share this answer
 
Comments
Sunasara Imdadhusen 12-Nov-10 1:03am    
But he wants in C#.Net
There is an Article at CP itself.

Have a look
 
Share this answer
 
Hi
Please visit following link

how to compress and extract the folder in asp.net using c# coding[^]


Please do let me know, if you have any doubt.

Please provide "Vote" if this would be helpful, and make "Accept Answer" if this would be good link.

Thanks,
Imdadhusen
 
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