Unzipping folder using C#.Net

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,

I have written a class library to unzip the zipped file when the source zip file path and destination is given. It performs the unzip operation and unzippes the folder. All the folders remain as it is after unzipping. But the contents of the files are being erased. The files have zero memory. For examples...if the zip files....Project1.Zip has been unzipped...it wud be like Project1>build>src>textfile1.txt...The textfile1.txt contents are being erased....and i have no idea why its doing that...Can u please go thru my class library once and tell me where it went wrong.....if not suggest me a different or easy why to do it or if there are any class library available on the internet which i can directly add to my application.

using System;
using System.Collections.Generic;
using System.Linq;
using java.util;
using java.io;
using System.Text;
using java.util.zip;
using System.IO;

namespace BatchProcessing.ZipFiles
{
    public class Zipfiles
    {
        FileInputStream fileinput;
        FileOutputStream fileoutput = null;
        string path = null;
        ZipOutputStream zipoutput = null;

       private void Init()
        {
            fileoutput = new FileOutputStream(path);
            zipoutput = new ZipOutputStream(this.zipoutput);
            fileinput = null;
        }
       
        private void CloseStreams()
        {
            zipoutput.closeEntry();
            fileinput.close();
            zipoutput.close();
            fileoutput.close();
           
        }
       
        public void UnzipFiles(string zippedfile, string destinationdirectory)
        {
            ZipFile zipfile = new ZipFile(zippedfile);
            List<ZipEntry> zippedFileList = new List<ZipEntry>();
            Enumeration enumZip = zipfile.entries();
            while (enumZip.hasMoreElements())
            {
                ZipEntry zipped = (ZipEntry)enumZip.nextElement();
                zippedFileList.Add(zipped);
            }
            foreach (ZipEntry zippedFile in zippedFileList)
            {
                if (!zippedFile.isDirectory())
                {
                    InputStream input = zipfile.getInputStream(zippedFile);
                    String s = (Directory.CreateDirectory(destinationdirectory + @"" + Path.GetDirectoryName(zippedFile.getName()))).ToString();
                    FileOutputStream outputpath = new FileOutputStream(Path.Combine(destinationdirectory + @"" + Path.GetDirectoryName(zippedFile.getName()), Path.GetFileName(zippedFile.getName())));
 
                    int length = 0;
                    sbyte[] result = new sbyte[10204];
                    while ((length = input.read(result)) >= 0)
                    outputpath.close();
                    input.close();
                    {
                        outputpath.write(result, 0, length);
                    }

                }
            }
        }
        public void ZipFiles(String indFileName, string[] sourceFiles)
        {
            Init();
            path = indFileName;
            foreach (string filename in sourceFiles)
            {
                fileinput = new FileInputStream(filename);
                ZipEntry zipEntry = new ZipEntry(Path.GetFileName(filename));
                zipoutput.putNextEntry(zipEntry);
                sbyte[] result = new sbyte[512];
                int length = 0;
                while ((length = fileinput.read(result)) >= 0)
                {
                    zipoutput.write(result, 0, length);
                }
            }
            CloseStreams();
        }

       
    }
}



thank You,
Suman


View the full article
 
Back
Top