示例#1
0
 public static void DecompressionAsyn(string inFile, string outFile, unzipProgressDelegate progress)
 {
     unzipDecompressionDelegate mydelegate = new unzipDecompressionDelegate(Decompress);
     IAsyncResult result = mydelegate.BeginInvoke(inFile, outFile, progress, AsyncCallbackMethod, mydelegate);
 }
示例#2
0
    /// <summary>
    /// 解压缩
    /// </summary>
    /// <param name="sourceFile">源文件</param>
    /// <param name="targetPath">目标路经</param>
    public static bool Decompress(string sourceFile, string targetPath, unzipProgressDelegate deleg)
    {
        errorMsg = "";
        long zipFileCount = 0;

        using (ZipFile zip = new ZipFile(sourceFile))
        {
            zipFileCount = zip.Count;
            zip.Close();
        }
        if (!File.Exists(sourceFile))
        {
            throw new FileNotFoundException(string.Format("未能找到文件 '{0}' ", sourceFile));
        }
        if (!Directory.Exists(targetPath))
        {
            Directory.CreateDirectory(targetPath);
        }

        using (ZipInputStream s = new ZipInputStream(File.OpenRead(sourceFile)))
        {
            int      unZipCount = 0;
            ZipEntry theEntry;
            while ((theEntry = s.GetNextEntry()) != null)
            {
                try
                {
                    string directorName = Path.Combine(targetPath, Path.GetDirectoryName(theEntry.Name));
                    string fileName     = directorName + "/" + Path.GetFileName(theEntry.Name);
                    if (File.Exists(fileName))
                    {
                        File.Delete(fileName);
                    }
                    // 创建目录
                    if (Directory.Exists(directorName) == false && directorName.Length > 0)
                    {
                        Directory.CreateDirectory(directorName);
                    }
                    if (fileName != string.Empty)
                    {
                        using (FileStream streamWriter = File.Create(fileName))
                        {
                            int    size = 4096;
                            byte[] data = new byte[4 * 1024];
                            while (true)
                            {
                                size = s.Read(data, 0, data.Length);
                                if (size > 0)
                                {
                                    streamWriter.Write(data, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                                //deleg((float)((double)s.Position / s.Length));
                            }
                            streamWriter.Close();
                            unZipCount++;
                        }
                    }
                    if (deleg != null)
                    {
                        deleg((float)((float)unZipCount / (float)zipFileCount));
                    }
                }
                catch (Exception ex)
                {
                    errorMsg = ex.Message;
                    return(false);
                }
            }
        }
        return(true);
    }