Beispiel #1
0
        private void WriteAnArchive(string tarFileName)
        {
            using (FileStream ms = File.OpenWrite(tarFileName))
            {
                Assert.AreEqual(0, ms.Length);
                writer.SetBaseStream(ms);

                using (MemoryStream ms1 = new MemoryStream(Encoding.ASCII.GetBytes("This is a test")))
                {
                    writer.Add(ms1, "tf1");
                }

                using (MemoryStream ms2 = new MemoryStream(Encoding.ASCII.GetBytes("This is a test")))
                {
                    writer.Add(ms2, "adir2/a/tf2", DateTime.Now);
                }

                writer.AddDirectory("adir/");
                writer.AddDirectory("adir2/a", DateTime.Now);
                writer.Dispose();
            }

            Assert.IsTrue(File.Exists(tarFileName), "archive exists");
            Assert.IsTrue(File.ReadAllBytes(tarFileName).Length > 0, "archive has contents");
        }
Beispiel #2
0
        public void Archive_Skip()
        {
            var dir    = GetResultsWith("Ignore");
            var ignore = IO.Combine(dir, "Sample.txt");

            var io = new IO();

            io.Failed += (s, e) => e.Cancel = true;
            io.Copy(GetExamplesWith("Sample.txt"), ignore);

            var dest = io.Combine(dir, "Sample.zip");

            using (var _ = OpenExclude(ignore))
                using (var writer = new ArchiveWriter(Format.Zip, io))
                {
                    writer.Add(ignore);
                    writer.Add(GetExamplesWith("Sample 00..01"));
                    writer.Save(dest);
                }

            using (var reader = new ArchiveReader(dest))
            {
                Assert.That(reader.Items.Count, Is.EqualTo(8));
                Assert.That(reader.Items.Any(x => x.FullName == "Sample.txt"), Is.False);
            }
        }
Beispiel #3
0
 public void Add_NotFound() => Assert.That(() =>
 {
     using (var writer = new ArchiveWriter(Format.Zip))
     {
         writer.Add(GetExamplesWith("NotFound.txt"));
     }
 }, Throws.TypeOf <System.IO.FileNotFoundException>());
        /* ----------------------------------------------------------------- */
        ///
        /// Archive
        ///
        /// <summary>
        /// 圧縮処理を実行します。
        /// </summary>
        ///
        /* ----------------------------------------------------------------- */
        private void Archive()
        {
            var fmt   = GetFormat();
            var dest  = GetTmp();
            var query = RtSettings.Password.HasValue() || Request.Password ?
                        new Query <string>(e => RaisePasswordRequested(e)) :
                        null;

            System.Diagnostics.Debug.Assert(RtSettings != null);
            this.LogDebug(string.Format("Format:{0}\tMethod:{1}", fmt, RtSettings.CompressionMethod));

            using (var writer = new ArchiveWriter(fmt, IO))
            {
                writer.Option = RtSettings.ToOption(Settings);
                if (Settings.Value.Archive.Filtering)
                {
                    writer.Filters = Settings.Value.GetFilters();
                }
                foreach (var item in Request.Sources)
                {
                    writer.Add(item);
                }
                ProgressStart();
                writer.Save(dest, query, CreateInnerProgress(x => Report = x));
            }

            // Move
            if (!IO.Exists(Tmp))
            {
                return;
            }
            IO.Move(Tmp, Destination, true);
        }
Beispiel #5
0
        public int Archive_Filter(bool filter)
        {
            var names = new[] { "Filter.txt", "FilterDirectory" };
            var s     = filter ? "True" : "False";
            var dest  = GetResultsWith($"Filter{s}.zip");

            using (var writer = new ArchiveWriter(Format.Zip))
            {
                if (filter)
                {
                    writer.Filters = names;
                }
                writer.Add(GetExamplesWith("Sample.txt"));
                writer.Add(GetExamplesWith("Sample 00..01"));
                writer.Save(dest);
            }

            using (var reader = new ArchiveReader(dest)) return(reader.Items.Count);
        }
Beispiel #6
0
 public void Archive_PasswordCancel() => Assert.That(() =>
 {
     using (var writer = new ArchiveWriter(Format.Zip))
     {
         var dest  = GetResultsWith("PasswordCancel.zip");
         var query = new Query <string>(e => e.Cancel = true);
         writer.Add(GetExamplesWith("Sample.txt"));
         writer.Save(dest, query, null);
     }
 }, Throws.TypeOf <OperationCanceledException>());
Beispiel #7
0
 public void Archive_SfxNotFound() => Assert.That(() =>
 {
     using (var writer = new ArchiveWriter(Format.Sfx))
     {
         var dest      = GetResultsWith("SfxNotFound.exe");
         writer.Option = new SfxOption {
             Module = "dummy.sfx"
         };
         writer.Add(GetExamplesWith("Sample.txt"));
         writer.Save(dest);
     }
 }, Throws.TypeOf <System.IO.FileNotFoundException>());
Beispiel #8
0
        public void Archive_PermissionError() => Assert.That(() =>
        {
            var dir = GetResultsWith("PermissionError");
            var src = IO.Combine(dir, "Sample.txt");

            IO.Copy(GetExamplesWith("Sample.txt"), src);

            using (var _ = OpenExclude(src))
                using (var writer = new ArchiveWriter(Format.Zip))
                {
                    writer.Add(src);
                    writer.Save(IO.Combine(dir, "Sample.zip"));
                }
        }, Throws.TypeOf <System.IO.IOException>());
        public Format Archive(Format format, string filename, string password,
                              string[] items, ArchiveOption option)
        {
            var dest = GetResultsWith(filename);

            using (var writer = new ArchiveWriter(format))
            {
                writer.Option = option;
                foreach (var item in items)
                {
                    writer.Add(GetExamplesWith(item));
                }
                writer.Save(dest, password);
                writer.Clear();
            }

            using (var ss = IO.OpenRead(dest)) return(Formats.FromStream(ss));
        }
        /* ----------------------------------------------------------------- */
        ///
        /// Invoke
        ///
        /// <summary>
        /// Invokes the compression and saves the archive.
        /// </summary>
        ///
        /* ----------------------------------------------------------------- */
        private void Invoke(CompressRuntimeSetting src)
        {
            GetType().LogDebug($"Format:{src.Format}", $"Method:{src.CompressionMethod}");

            using (var writer = new ArchiveWriter(src.Format, src.ToOption(Settings)))
            {
                foreach (var e in Request.Sources)
                {
                    writer.Add(e);
                }
                writer.Save(Temp, GetProgress());
            }

            if (Io.Exists(Temp))
            {
                Io.Move(Temp, Destination, true);
            }
        }
Beispiel #11
0
        public void Archive_Japanese(bool utf8)
        {
            var fmt  = Format.Zip;
            var src  = GetResultsWith("日本語のファイル名.txt");
            var code = utf8 ? "UTF8" : "SJis";
            var dest = GetResultsWith($"ZipJapanese{code}.zip");

            IO.Copy(GetExamplesWith("Sample.txt"), src, true);
            Assert.That(IO.Exists(src), Is.True);

            using (var writer = new ArchiveWriter(fmt))
            {
                writer.Option = new ZipOption {
                    CodePage = utf8 ? CodePage.Utf8 : CodePage.Japanese
                };
                writer.Add(src);
                writer.Save(dest);
            }

            using (var stream = System.IO.File.OpenRead(dest))
            {
                Assert.That(Formats.FromStream(stream), Is.EqualTo(fmt));
            }
        }
        protected override void Run()
        {
            // The directory in which we assemble the log files from the server before repackaging them
            // in a single zip file.
            string extractTempDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            try
            {
                // Calculate total bytes to save
                long bytesToExtract = 1, bytesExtracted = 0;
                foreach (string inputFile in Directory.GetFiles(_inputTempFolder))
                {
                    bytesToExtract += new FileInfo(inputFile).Length;
                }

                // Create temp dir for extracted stuff
                if (Directory.Exists(extractTempDir))
                {
                    Directory.Delete(extractTempDir);
                }
                Directory.CreateDirectory(extractTempDir);

                // Extract each of the raw server files to the temp extraction directory
                foreach (string inputFile in Directory.GetFiles(_inputTempFolder))
                {
                    if (inputFile.ToLowerInvariant().EndsWith(".tar"))
                    {
                        // Un-tar it. SharpZipLib doesn't account for illegal filenames or characters in
                        // filenames (e.g. ':'in Windows), so first we stream the tar to a new tar,
                        // sanitizing any bad filenames as we go.

                        // We also need to record the modification times of all the files, so that we can
                        // restore them into the final zip.

                        string outFilename = inputFile.Substring(0, inputFile.Length - 4);
                        if (outFilename.Length == 0)
                        {
                            outFilename = Path.GetRandomFileName();
                        }
                        string outputDir = Path.Combine(extractTempDir, Path.GetFileName(outFilename));

                        string sanitizedTar = Path.GetTempFileName();

                        using (ArchiveIterator tarIterator = ArchiveFactory.Reader(ArchiveFactory.Type.Tar, File.OpenRead(inputFile)))
                        {
                            using (ArchiveWriter tarWriter = ArchiveFactory.Writer(ArchiveFactory.Type.Tar, File.OpenWrite(sanitizedTar)))
                            {
                                Dictionary <string, string> usedNames = new Dictionary <string, string>();
                                while (tarIterator.HasNext())
                                {
                                    if (Cancelling)
                                    {
                                        throw new CancelledException();
                                    }

                                    using (MemoryStream ms = new MemoryStream())
                                    {
                                        tarIterator.ExtractCurrentFile(ms);
                                        string saneName = SanitizeTarName(tarIterator.CurrentFileName(), usedNames);
                                        tarWriter.Add(ms, saneName);
                                        ModTimes[Path.Combine(outputDir, saneName)] = tarIterator.CurrentFileModificationTime();
                                    }
                                }
                            }
                        }

                        // Now extract the sanitized tar
                        using (FileStream fs = File.OpenRead(sanitizedTar))
                        {
                            using (ArchiveIterator tarIterator = ArchiveFactory.Reader(ArchiveFactory.Type.Tar, fs))
                            {
                                Directory.CreateDirectory(outputDir);
                                tarIterator.ExtractAllContents(outputDir);
                                bytesToCompress += Core.Helpers.GetDirSize(new DirectoryInfo(outputDir));
                            }
                        }
                    }
                    else
                    {
                        // Just copy vanilla input files unmodified to the temp directory
                        string outputFile = Path.Combine(extractTempDir, Path.GetFileName(inputFile));
                        File.Copy(inputFile, outputFile);
                        ModTimes[outputFile] = new FileInfo(inputFile).LastWriteTimeUtc;
                        bytesToCompress     += new FileInfo(outputFile).Length;
                    }

                    bytesExtracted += new FileInfo(inputFile).Length;
                    File.Delete(inputFile);
                    this.PercentComplete = (int)(50.0 * bytesExtracted / bytesToExtract);

                    if (Cancelling)
                    {
                        throw new CancelledException();
                    }
                }

                // Now zip up all the temporarily extracted files into a single zip file for the user
                log.DebugFormat("Packing {0} of bug report files into zip file {1}",
                                Util.DiskSizeString(bytesToCompress), _destFile);

                LogDescriptionChanges = false;
                try
                {
                    ZipToOutputFile(extractTempDir);
                    PercentComplete = 100;

                    // Only cleanup files if it succeeded (or cancelled)
                    CleanupFiles(extractTempDir);
                }
                finally
                {
                    LogDescriptionChanges = true;
                }

                if (Cancelling)
                {
                    throw new CancelledException();
                }
            }
            catch (CancelledException)
            {
                CleanupFiles(extractTempDir, true);

                throw;
            }
            catch (Exception exn)
            {
                ZipToOutputFile(_inputTempFolder);
                PercentComplete = 100;
                log.ErrorFormat("An exception was trapped while creating a server status report: " + exn.Message);
                throw new Exception(Messages.STATUS_REPORT_ZIP_FAILED);
            }
        }