Exemplo n.º 1
0
 public static void unzipFile(Stream inputStream, string tmpDirectory, string targetDirectory, OnUnzipProgress callback)
 {
     _inputStream     = inputStream;
     _tmpDirectory    = tmpDirectory;
     _targetDirectory = targetDirectory;
     _callback        = callback;
     new Thread(new ThreadStart(unzipFileThread)).Start();
 }
Exemplo n.º 2
0
 void start(int totalBytes, OnUnzipProgress onUnzipProgress, bool overwrite = true)
 {
     this.totalBytes      = totalBytes;
     this.onUnzipProgress = onUnzipProgress;
     this.overwrite       = overwrite;
     setState(State.Running);
     thread = new Thread(new ThreadStart(writeThreadFunc));
     thread.Start();
 }
Exemplo n.º 3
0
 public void ExtractZipAsync(Stream inputStream, string tmpDirectory, string targetDirectory, OnUnzipProgress callback)
 {
     mInputStream  = inputStream;
     mTmpDirectory = tmpDirectory;
     mOutDirectory = targetDirectory;
     mCallback     = callback;
     mUnzipBytes   = 0;
     mUpdateBytes  = 0;
     mUnzipSize    = 0;
     mCallback(0, 0);
     new Thread(new ThreadStart(ExtractZipThread)).Start();
 }
Exemplo n.º 4
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;
            }
        }
Exemplo n.º 5
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);
            }
        }