Click here to Skip to main content
11,482,402 members (73,790 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: file Message
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 6-Sep-12 3:22am
Ali_1001.4K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

using(ZipInputStream stream = new ZipInputStream(File.OpenRead(@"c:\DoUnZip.zip"));){
      // do your work here
   }
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try this code what i have used in my application and it is working fine :-

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);
  Permalink  

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



Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 6 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100