Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
The method is here,
The file name extract which is unziped successfully, but when i goto the directory & try to delete it,but it says it is using by another program,
what i got missing?

public static void UnZipFiles()
   {
       ZipInputStream stream = new ZipInputStream(File.OpenRead(@"c:\DoUnZip.zip"));
       ZipEntry theEntry;
       FileStream streamWriter = null;
       try
       {
           string tmpEntry = String.Empty;
           while ((theEntry = stream.GetNextEntry()) != null)
           {
               string directoryName = @"d:\extract";
               string fileName = Path.GetFileName(theEntry.Name);
               // create directory
               if (directoryName != "")
               {
               System.IO.Directory.CreateDirectory(directoryName);
               }
               if (fileName != String.Empty)
               {
                   if (theEntry.Name.IndexOf(".ini") < 0)
                   {
                       string fullPath = directoryName + "\\" + theEntry.Name;
                       fullPath = fullPath.Replace("\\ ", "\\");
                       string fullDirPath = Path.GetDirectoryName(fullPath);
                       if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
                        streamWriter = File.Create(fullPath);
                       int size = 2048;
                       byte[] data = new byte[size];
                       while (true)
                       {
                           size = stream.Read(data, 0, data.Length);
                           if (size > 0)
                           {
                               streamWriter.Write(data, 0, size);
                           }
                           else
                           {
                               break;
                           }
                       }
                   }
               }
           }
       }
Posted

Use File.Close() or enclose the code inside the using blocks like this

C#
using(ZipInputStream stream = new ZipInputStream(File.OpenRead(@"c:\DoUnZip.zip"));){
      // do your work here
   }
 
Share this answer
 
v2
Try this code what i have used in my application and it is working fine :-

C#
public string Zip(string ZipFileName)
    {
        try
        {
            if (!File.Exists(ZipFileName))
            {
                //lstProcess.Items.Add("File Does Not Exists.");
                return "";
            }

            using (ZipInputStream s = new ZipInputStream(File.OpenRead(ZipFileName)))
            {
                string fileName = "";
                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    string directoryName = Path.GetDirectoryName(theEntry.Name);
                    fileName = Path.GetFileName(theEntry.Name);

                    // create directory
                    if (directoryName.Length > 0)
                    {
                        Directory.CreateDirectory(Server.MapPath("") + "\\XML\\" + directoryName);
                    }

                    if (fileName != String.Empty)
                    {
                        using (FileStream streamWriter = File.Create(Server.MapPath("") + "\\XML\\" + theEntry.Name))
                        {

                            int size = 2048;
                            byte[] data = new byte[2048];
                            while (true)
                            {
                                size = s.Read(data, 0, data.Length);
                                if (size > 0)
                                {
                                    streamWriter.Write(data, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
                return fileName;
            }
        }
        catch (Exception ex)
        {
            //Display Error
            return "";
        }
        finally
        {
            //Update Status
        }
    }



calling to this function
------------------------------
string extractFileName = Server.MapPath("") + "\\XML\\" + Zip(FilePath);
 
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