Esempio n. 1
0
        public void ZipFileInfoNullParams()
        {
            Exception caughtEx;
            ZipInfo   zipInfo = new ZipInfo("test.zip");
            int       txtSize = 10240;

            CompressionTestUtil.GenerateRandomFile("test00.txt", 0, txtSize);
            CompressionTestUtil.GenerateRandomFile("test01.txt", 1, txtSize);
            zipInfo.PackFiles(null, new string[] { "test00.txt", "test01.txt" }, null);
            ZipFileInfo zfi = new ZipFileInfo(zipInfo, "test01.txt");

            caughtEx = null;
            try
            {
                new ZipFileInfo(null, "test00.txt");
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException));
            caughtEx = null;
            try
            {
                new ZipFileInfo(zipInfo, null);
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException));
            caughtEx = null;
            try
            {
                zfi.CopyTo(null);
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException));
        }
Esempio n. 2
0
        public void ZipInfoGetFiles()
        {
            IList <ZipFileInfo> fileInfos;
            ZipInfo             zipInfo = new ZipInfo("testgetfiles.zip");

            int txtSize = 10240;

            CompressionTestUtil.GenerateRandomFile("testinfo0.txt", 0, txtSize);
            CompressionTestUtil.GenerateRandomFile("testinfo1.txt", 1, txtSize);
            CompressionTestUtil.GenerateRandomFile("testinfo2.ini", 2, txtSize);
            zipInfo.PackFiles(null, new string[] { "testinfo0.txt", "testinfo1.txt", "testinfo2.ini" }, null);

            fileInfos = zipInfo.GetFiles();
            Assert.IsNotNull(fileInfos);
            Assert.AreEqual <int>(3, fileInfos.Count);
            Assert.AreEqual <string>("testinfo0.txt", fileInfos[0].Name);
            Assert.AreEqual <string>("testinfo1.txt", fileInfos[1].Name);
            Assert.AreEqual <string>("testinfo2.ini", fileInfos[2].Name);

            fileInfos = zipInfo.GetFiles("*.txt");
            Assert.IsNotNull(fileInfos);
            Assert.AreEqual <int>(2, fileInfos.Count);
            Assert.AreEqual <string>("testinfo0.txt", fileInfos[0].Name);
            Assert.AreEqual <string>("testinfo1.txt", fileInfos[1].Name);

            fileInfos = zipInfo.GetFiles("testinfo1.txt");
            Assert.IsNotNull(fileInfos);
            Assert.AreEqual <int>(1, fileInfos.Count);
            Assert.AreEqual <string>("testinfo1.txt", fileInfos[0].Name);
            Assert.IsTrue(DateTime.Now - fileInfos[0].LastWriteTime < TimeSpan.FromMinutes(1),
                          "Checking ZipFileInfo.LastWriteTime is current.");
        }
Esempio n. 3
0
        public void ZipInfoNullParams()
        {
            int    fileCount = 10, fileSize = 1024;
            string dirA = String.Format("{0}-{1}-A", fileCount, fileSize);

            if (Directory.Exists(dirA))
            {
                Directory.Delete(dirA, true);
            }
            Directory.CreateDirectory(dirA);
            string dirB = String.Format("{0}-{1}-B", fileCount, fileSize);

            if (Directory.Exists(dirB))
            {
                Directory.Delete(dirB, true);
            }
            Directory.CreateDirectory(dirB);

            string[] files = new string[fileCount];
            for (int iFile = 0; iFile < fileCount; iFile++)
            {
                files[iFile] = "zipinfo-" + iFile + ".txt";
                CompressionTestUtil.GenerateRandomFile(Path.Combine(dirA, files[iFile]), iFile, fileSize);
            }

            ZipInfo zipInfo = new ZipInfo("testnull.zip");

            CompressionTestUtil.TestArchiveInfoNullParams(zipInfo, dirA, dirB, files);
        }
Esempio n. 4
0
 /// <summary>
 /// 解压线程
 /// </summary>
 static void DecompressThread()
 {
     while (m_waitingList.Count > 0)
     {
         string src;
         string dst;
         lock (m_waitingList)
         {
             ZipInfo zipInfo = m_waitingList.Dequeue();
             src = zipInfo.src;
             dst = zipInfo.dst;
         }
         m_size     = 0;
         m_progress = 0;
         m_sum      = 0;
         //计算zip解压后的大小
         using (ZipFile zip = new ZipFile(src))
         {
             foreach (ZipEntry ze in zip)
             {
                 m_size += ze.Size;
             }
         }
         Decompress(src, dst, false);
         m_progress = 1;
     }
     m_isRunning = false;
     m_thread.Abort();
 }
Esempio n. 5
0
        public async Task <ZipInfo> GetZipDetail(string country, string zip)
        {
            IpController zc = new IpController();

            if (country == "" || country == null)
            {
                var apiIP = zc.GetIPApiAddress();
                country = apiIP.Result.Country;
            }
            if (zip == "" || zip == null)
            {
                var apiIP = zc.GetIPApiAddress();
                zip = apiIP.Result.Zip;
            }

            ZipInfo zipInfo = null;

            using (var client = new HttpClient())
            {
                var json1 = await client.GetStringAsync("http://api.zippopotam.us/" + country + "/" + zip);;
                zipInfo = JsonSerializer.Deserialize <ZipInfo>(json1, new JsonSerializerOptions {
                    PropertyNameCaseInsensitive = true
                });
            }
            return(zipInfo);
        }
Esempio n. 6
0
        public void ZipTruncatedArchive()
        {
            ZipInfo zipInfo = new ZipInfo("test-t.zip");

            CompressionTestUtil.GenerateRandomFile("ziptest-0.txt", 0, 5);
            CompressionTestUtil.GenerateRandomFile("ziptest-1.txt", 1, 5);
            zipInfo.PackFiles(null, new string[] { "ziptest-0.txt", "ziptest-1.txt" }, null);

            CompressionTestUtil.TestTruncatedArchive(zipInfo, typeof(ZipException));
        }
Esempio n. 7
0
        public void ZipBadUnpackStreamContexts()
        {
            int     txtSize = 40960;
            ZipInfo zipInfo = new ZipInfo("test2.zip");

            CompressionTestUtil.GenerateRandomFile("ziptest-0.txt", 0, txtSize);
            CompressionTestUtil.GenerateRandomFile("ziptest-1.txt", 1, txtSize);
            zipInfo.PackFiles(null, new string[] { "ziptest-0.txt", "ziptest-1.txt" }, null);

            using (ZipEngine zipEngine = new ZipEngine())
            {
                CompressionTestUtil.TestBadUnpackStreamContexts(zipEngine, "test2.zip");
            }
        }
Esempio n. 8
0
        private IEnumerable <string> _UnpackArchive(string localFilename, string destinationFolder, IRequestObject requestObject)
        {
            var info      = new ZipInfo(localFilename);
            var files     = info.GetFiles();
            var percent   = 0;
            var index     = 0;
            var processed = new List <string>();

            using (var request = requestObject.As <Request>()) {
                // request.Debug("Unpacking {0} {1}", localFilename, destinationFolder);
                var pid = request.StartProgress(0, "Unpacking Archive '{0}' ", Path.GetFileName(localFilename));
                try {
                    info.Unpack(destinationFolder, (sender, args) => {
                        if (args.ProgressType == ArchiveProgressType.FinishFile)
                        {
                            processed.Add(Path.Combine(destinationFolder, args.CurrentFileName));
                            index++;
                            var complete = (index * 100) / files.Count;
                            if (complete != percent)
                            {
                                percent = complete;
                                request.Progress(pid, percent, "Unpacked {0}", args.CurrentFileName);
                            }

                            /*
                             * Does not currently support cancellation .
                             * Todo: add cancellation support to DTF compression classes.
                             * */
                            if (request.IsCanceled)
                            {
                                throw new OperationCanceledException("cancelling");
                            }
                        }
                    });
                } catch (OperationCanceledException) {
                    // no worries.
                } catch (Exception e) {
                    e.Dump();
                }

                // request.Debug("DONE Unpacking {0} {1}", localFilename, destinationFolder);
                request.CompleteProgress(pid, true);
            }
            // return the list of files to the parent.
            return(processed.ToArray());
        }
Esempio n. 9
0
        public void Compress(string filePath, bool deleteSourceFolder)
        {
            var filePaths = new List <string>();

            if (Directory.Exists(filePath))
            {
                filePaths.AddRange(Directory.GetFileSystemEntries(filePath).ToList());
            }
            if (filePaths.Count > 0)
            {
                var zip = new ZipInfo(_zipfilepath);
                zip.Pack(filePath, true, CompressionLevel.None, null);
            }
            if (deleteSourceFolder)
            {
                Directory.Delete(filePath, deleteSourceFolder);
            }
        }
Esempio n. 10
0
        internal static void CompressFile(string filePath, bool deleteOriginal = false)
        {
            if (File.Exists(filePath))
            {
                var compressedFilePath = filePath + ".zip";
                var zipFile            = new ZipInfo(compressedFilePath);
                var files = new Collection <string>()
                {
                    filePath
                };
                zipFile.PackFiles(null, files, null, CompressionLevel.Max, null);

                if (deleteOriginal && File.Exists(compressedFilePath))
                {
                    File.Delete(filePath);
                }
            }
        }
Esempio n. 11
0
        public static ZipInfo ToZipInfo(this EFDAL.Entity.Zip zip)
        {
            var retval = new ZipInfo()
            {
                ID      = zip.ZipId,
                Name    = zip.Name,
                City    = zip.City,
                State   = zip.State,
                Country = zip.Country ?? "US",
            };

            if (zip.Latitude != null && zip.Longitude != null)
            {
                retval.Latitude  = zip.Latitude.Value;
                retval.Longitude = zip.Longitude.Value;
            }
            return(retval);
        }
Esempio n. 12
0
 /// <summary>
 /// This methods extract a zip file in the given directory
 /// </summary>
 /// <param name="filePath"></param>
 /// <param name="targetDir"></param>
 public static bool ExtractAll(string filePath, string targetDir)
 {
     if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
     {
         return(false);
     }
     if (!CreateDirectory(targetDir))
     {
         return(false);
     }
     try {
         ZipInfo zip = new ZipInfo(filePath);
         zip.Unpack(targetDir);
     } catch (Exception e) {
         ErrorHandler.ShowErrors(e, "Unzipping " + Path.GetFileName(filePath));
         return(false);
     }
     return(true);
 }
Esempio n. 13
0
        public ZipInfo GetZip(string term)
        {
            if (string.IsNullOrEmpty(term))
            {
                return(null);
            }

            var cacheHit = true;
            var timer    = Stopwatch.StartNew();

            try
            {
                ZipInfo retval = null;
                if (!_cache.TryGetValue(term, out retval))
                {
                    cacheHit = false;
                    var item = Lookup(term).FirstOrDefault();
                    if (item == null)
                    {
                        return(null);
                    }
                    retval = item.ToZipInfo();
                    _cache.TryAdd(term, retval);
                }
                return(retval);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                return(null);
            }
            finally
            {
                timer.Stop();
                Logger.LogInfo($"GetZip: Term={term}, Elapsed={timer.ElapsedMilliseconds}, cacheHit={cacheHit}");
            }
        }
        private static void Uncompress(string path, string output)
        {
            ZipInfo z = new ZipInfo(path);

            z.Unpack(output);
        }
Esempio n. 15
0
        public void Uncompress(string destinationPath)
        {
            var zip = new ZipInfo(_zipfilepath);

            zip.Unpack(destinationPath);
        }
Esempio n. 16
0
        private IEnumerable<string> _UnpackArchive(string localFilename, string destinationFolder, Request request) {
            var info = new ZipInfo(localFilename);
            var files = info.GetFiles();
            var percent = 0;
            var index = 0;
            var processed = new List<string>();

                // request.Debug("Unpacking {0} {1}", localFilename, destinationFolder);
                var pid = request.StartProgress(0, "Unpacking Archive '{0}' ", Path.GetFileName(localFilename));
                try {
                    info.Unpack(destinationFolder, (sender, args) => {
                        if (args.ProgressType == ArchiveProgressType.FinishFile) {
                            processed.Add(Path.Combine(destinationFolder, args.CurrentFileName));
                            index++;
                            var complete = (index*100)/files.Count;
                            if (complete != percent) {
                                percent = complete;
                                request.Progress(pid, percent, "Unpacked {0}", args.CurrentFileName);
                            }
                            /*
                         * Does not currently support cancellation .
                         * Todo: add cancellation support to DTF compression classes.
                         * */
                            if (request.IsCanceled) {
                                throw new OperationCanceledException("cancelling");
                            }
                        }
                    });
                } catch (OperationCanceledException) {
                    // no worries.
                }

                // request.Debug("DONE Unpacking {0} {1}", localFilename, destinationFolder);
                request.CompleteProgress(pid, true);

            // return the list of files to the parent.
            return processed.ToArray();
        }
Esempio n. 17
0
        private IEnumerable <string> _UnpackArchive(string localFilename, string destinationFolder, Object requestImpl)
        {
            var info      = new ZipInfo(localFilename);
            var files     = info.GetFiles();
            var percent   = 0;
            var index     = 0;
            var processed = new List <string>();

            using (var request = requestImpl.As <Request>()) {
                // request.Debug("Unpacking {0} {1}", localFilename, destinationFolder);
                var pid = request.StartProgress(0, "Unpacking Archive '{0}' ", Path.GetFileName(localFilename));
                try {
                    info.Unpack(destinationFolder, (sender, args) => {
                        if (args.ProgressType == ArchiveProgressType.FinishFile)
                        {
                            processed.Add(Path.Combine(destinationFolder, args.CurrentFileName));
                            index++;
                            var complete = (index * 100) / files.Count;
                            if (complete != percent)
                            {
                                percent = complete;
                                request.Progress(pid, percent, "Unpacked {0}", args.CurrentFileName);
                            }

                            /*
                             * Does not currently support cancellation .
                             * Todo: add cancellation support to DTF compression classes.
                             * */
                            if (request.IsCancelled())
                            {
                                throw new OperationCanceledException("cancelling");
                            }
                        }
                    });
                } catch (OperationCanceledException) {
                    // no worries.
                } catch (Exception e) {
                    e.Dump();
                }

                // request.Debug("DONE Unpacking {0} {1}", localFilename, destinationFolder);
                request.CompleteProgress(pid, true);
            }
            // return the list of files to the parent.
            return(processed.ToArray());

#if manually
            var result = new CancellableBlockingCollection <string>();
            Task.Factory.StartNew(() => {
                using (var request = requestImpl.As <Request>()) {
                    request.Debug("Unpacking {0} {1}", localFilename, destinationFolder);

                    var info  = new ZipInfo(localFilename);
                    var files = info.GetFiles();
                    request.Debug("File Count", files.Count);

                    var percent = 0;
                    var index   = 0;

                    foreach (var zipfile in info)
                    {
                        index++;
                        var complete = (index * 100) / files.Count;
                        if (complete != percent)
                        {
                            percent = complete;
                            request.Debug("Percent Complete {0}", percent);
                        }
                        var outputFilename = Path.Combine(destinationFolder, zipfile.Path, zipfile.Name);
                        zipfile.CopyTo(outputFilename, true);
                        result.Add(outputFilename);
                    }
                    request.Debug("DONE Unpacking {0} {1}", localFilename, destinationFolder);
                    result.CompleteAdding();
                }
            });
            return(CancellableBlockingCollection <string> .ToCancellableEnumerable(result));
#endif

            /*
             * var info = new ZipInfo(localFilename);
             *
             * foreach (var zipfile in info.GetFiles()) {
             *  var outputFilename = Path.Combine(destinationFolder, zipfile.Path, zipfile.Name);
             *  zipfile.CopyTo(outputFilename, true);
             *
             *  yield return outputFilename;
             * }
             * */
        }