Wednesday, 16 January 2013

Zip and Unzip File Using C# ASP.Net

Zip and Unzip File Using C# ASP.Net


To achieve this we have reference to J# class library..It resides in a file name "vjslib.dll"..

Right click your project in Server Explorer and click on "Add Reference" -> Select the .Net tab -> Scroll down and select "vjslib" -> Click OK and you are there. Now you can refer the Java library classes within your application. 

Include the following Namespaces:


using java.util;

using java.util.zip;

using java.io;

Then Use the following codes to achieve the functionality:


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 listZip = new List();

        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 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)) &gt;= 0)

                            {

                                dest.write(buffer, 0, len);

                            }

                        }

                        finally

                        {

                            dest.close();

                        }

                    }

                    finally

                    {

                        s.close();

                    }

                }

                sFlag = "Success";

            }

            return sFlag;

        }

        catch (Exception)

        {

            return sFlag;

        }

    }


Refer:

http://www.codeproject.com/Articles/18495/Simple-Application-to-Zip-and-UnZip-files-in-C-usi

No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top