Beispiel #1
0
        public static bool unzipFile(Stream inputStream, string targetDirectory, bool overwrite, OnUnzipProgress callback)
        {
            bool ret = false;

            callback(0, 0);
            using (ZipFile zipFile_ = new ZipFile(inputStream))
            {
                int       totalBytes = (int)zipFile_.unzipSize;
                UnzipCach cach       = new UnzipCach();
                cach.start(totalBytes, callback, overwrite);

                INameTransform extractNameTransform_      = new WindowsNameTransform(targetDirectory);
                System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    try
                    {
                        ZipEntry entry = (ZipEntry)enumerator.Current;
                        if (entry.IsFile)
                        {
                            string fileName = extractNameTransform_.TransformFile(entry.Name);
                            string dirName  = Path.GetDirectoryName(Path.GetFullPath(fileName));
                            if (!Directory.Exists(dirName))
                            {
                                Directory.CreateDirectory(dirName);
                            }
                            Stream source = zipFile_.GetInputStream(entry);
                            cach.addFile(fileName, (int)entry.Size, source);
                            source.Close();
                        }
                        else
                        {
                            string dirName = extractNameTransform_.TransformDirectory(entry.Name);
                            if (!Directory.Exists(dirName))
                            {
                                Directory.CreateDirectory(dirName);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        cach.setState(UnzipCach.State.Error);
                        //LogManager.GetInstance().LogException(e.Message, e, LogManager.ModuleFilter.RES);
                    }

                    if (cach.isError())
                    {
                        break;
                    }
                }

                cach.setState(UnzipCach.State.Ok);
                ret = cach.stop();
                callback(1, ret?0:1);
                return(ret);
            }
        }
Beispiel #2
0
        static void unzipFileThread()
        {
            //LogManager.GetInstance().LogMessage("unzip begin outpath="+_targetDirectory, LogManager.ModuleFilter.RES);
            _callback(0, 0);
            bool ret = false;

            using (ZipFile zipFile_ = new ZipFile(_inputStream))
            {
                int       totalBytes = (int)zipFile_.unzipSize;
                UnzipCach cach       = new UnzipCach();
                cach.start(totalBytes, _callback);

                INameTransform extractNameTransform_      = new WindowsNameTransform(_tmpDirectory);
                System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    try
                    {
                        ZipEntry entry = (ZipEntry)enumerator.Current;
                        if (entry.IsFile)
                        {
                            string fileName = extractNameTransform_.TransformFile(entry.Name);
                            string dirName  = Path.GetDirectoryName(Path.GetFullPath(fileName));
                            if (!Directory.Exists(dirName))
                            {
                                Directory.CreateDirectory(dirName);
                            }
                            Stream source = zipFile_.GetInputStream(entry);
                            cach.addFile(fileName, (int)entry.Size, source);
                            source.Close();
                        }
                        else
                        {
                            string dirName = extractNameTransform_.TransformDirectory(entry.Name);
                            if (!Directory.Exists(dirName))
                            {
                                Directory.CreateDirectory(dirName);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        cach.setState(UnzipCach.State.Error);
                        //LogManager.GetInstance().LogException(e.Message, e, LogManager.ModuleFilter.RES);
                    }

                    if (cach.isError())
                    {
                        break;
                    }
                }

                cach.setState(UnzipCach.State.Ok);
                if (cach.stop())
                {
                    try
                    {
                        Directory.Move(_tmpDirectory, _targetDirectory);
                        ret = true;
                    }
                    catch (Exception e)
                    {
                        //LogManager.GetInstance().LogException("unzip rename dir error.", e);
                    }
                }
                _callback(1, ret?0:1);
                //LogManager.GetInstance().LogMessage("unzip end", LogManager.ModuleFilter.RES);
                _inputStream = null;
                _callback    = null;
            }
        }