Beispiel #1
0
    void TarGzip(string path, string outputFile, bool insertCapitalizedOctoWrapper = false, bool insertCapitalizedDotNetWrapper = false)
    {
        var outFile = $"{outputFile}.tar.gz";

        Logger.Info("Creating TGZ file {0} from {1}", outFile, path);
        using (var tarMemStream = new MemoryStream())
        {
            using (var tar = WriterFactory.Open(tarMemStream, ArchiveType.Tar, new TarWriterOptions(CompressionType.None, true)))
            {
                // If using a capitalized wrapper, insert it first so it wouldn't overwrite the main payload on a case-insensitive system.
                if (insertCapitalizedOctoWrapper)
                {
                    tar.Write("Octo", AssetDirectory / "OctoWrapper.sh");
                }
                else if (insertCapitalizedDotNetWrapper)
                {
                    tar.Write("Octo", AssetDirectory / "octo");
                }

                // Add the remaining files
                tar.WriteAll(path, "*", SearchOption.AllDirectories);
            }

            tarMemStream.Seek(0, SeekOrigin.Begin);

            using (Stream stream = File.Open(outFile, FileMode.Create))
                using (var zip = WriterFactory.Open(stream, ArchiveType.GZip, CompressionType.GZip))
                {
                    zip.Write($"{outputFile}.tar", tarMemStream);
                }
        }

        Logger.Info("Successfully created TGZ file: {0}", outFile);
    }
        /// <summary>
        /// Create an archive from a directory. It will iterate through all directories recursively.
        /// </summary>
        /// <param name="folderPath">Folder/directory to archive</param>
        /// <param name="outputFile">Ouput path, including file name, of the resulting archive</param>
        /// <param name="useZip">true for zip format; false for tar.gz format</param>
        static public void CreateArchive(string folderPath, string outputFile, bool useZip)
        {
            using (var outputStream = File.OpenWrite(outputFile))
            {
                ArchiveType atype;
                var         cinfo = new CompressionInfo();

                if (useZip)
                {
                    atype      = ArchiveType.Zip;
                    cinfo.Type = CompressionType.Deflate;
                    cinfo.DeflateCompressionLevel = SharpCompress.Compressor.Deflate.CompressionLevel.Default;
                }
                else
                {
                    atype      = ArchiveType.Tar;
                    cinfo.Type = CompressionType.GZip;
                    cinfo.DeflateCompressionLevel = SharpCompress.Compressor.Deflate.CompressionLevel.Default;
                }

                using (var awriter = WriterFactory.Open(outputStream, atype, cinfo))
                {
                    awriter.WriteAll(folderPath, "*", SearchOption.AllDirectories);
                }
            }
        }
Beispiel #3
0
        public static async Task <FileCreationTaskResult> CreateArchiveFromFile(string file, string archiveFilePath, CancellationToken?token = null)
        {
            try
            {
                if (token == null)
                {
                    token = CancellationToken.None;
                }

                using (var zip = File.OpenWrite(archiveFilePath))
                    using (var zipWriter = WriterFactory.Open(zip, ArchiveType.Zip, CompressionType.Deflate))
                    {
                        Log.Here().Activity($"Saving {file} to archive at '{archiveFilePath}'.");
                        await WriteZipAsync(zipWriter, Path.GetFileName(file), file, token.Value);

                        return(FileCreationTaskResult.Success);
                    }
            }
            catch (Exception ex)
            {
                if (!token.Value.IsCancellationRequested)
                {
                    Log.Here().Error($"Error writing archive to '{archiveFilePath}': {ex.ToString()}");
                }
                else
                {
                    Log.Here().Warning($"Cancelled writing archive \"{archiveFilePath}\".");
                    return(FileCreationTaskResult.Skipped);
                }
            }
            return(FileCreationTaskResult.Error);
        }
Beispiel #4
0
            public void Pack(Stream outputZipStream, IEnumerable <AcCommonObject> objs)
            {
                var list        = objs.ToList();
                var description = PackedDescription.ToString(list.Select(GetDescriptionOverride));

                using (var writer = WriterFactory.Open(outputZipStream, ArchiveType.Zip, CompressionType.Deflate)) {
                    _writer = writer;
                    _added.Clear();

                    foreach (var obj in list)
                    {
                        Drain(Pack(obj), _cancellation);
                        if (_cancellation.IsCancellationRequested)
                        {
                            return;
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(description))
                    {
                        writer.WriteString("ReadMe.txt", description);
                    }
                }

                if (!string.IsNullOrEmpty(description))
                {
                    outputZipStream.AddZipDescription(description);
                }
            }
Beispiel #5
0
        public static void Compress(
            string entryPath,
            Stream inStream,
            Stream outStream,
            ArchiveType archiveType,
            CompressionType compressionType,
            ProgressStreamReportDelegate progressCallback)
        {
            char[] dirSeparators = { '\\', '/' };

            if (progressCallback != null)
            {
                outStream = new ProgressStream(outStream);
                ((ProgressStream)outStream).BytesWritten += progressCallback;
            }

            using (var writer = WriterFactory.Open(outStream, archiveType, new WriterOptions(compressionType)))
            {
                writer.Write(entryPath, inStream);
            }

            if (progressCallback != null)
            {
                ((ProgressStream)outStream).BytesWritten -= progressCallback;
            }
        }
Beispiel #6
0
        public static string BuildSamplePackage(string name, string version)
        {
            var sourceDirectory = TestEnvironment.GetTestPath("Fixtures", "Deployment", "Packages", name);

            var output = Path.Combine(Path.GetTempPath(), "CalamariTestPackages");

            Directory.CreateDirectory(output);

            var outputTarFilename = Path.Combine(output, name + "." + version + ".tar.gz");

            if (File.Exists(outputTarFilename))
            {
                File.Delete(outputTarFilename);
            }

            using (Stream stream = File.OpenWrite(outputTarFilename))
                using (var writer = WriterFactory.Open(stream, ArchiveType.Tar, new WriterOptions(CompressionType.GZip)
                {
                    LeaveStreamOpen = false
                }))
                {
                    writer.WriteAll(sourceDirectory, "*", SearchOption.AllDirectories);
                }

            return(outputTarFilename);
        }
Beispiel #7
0
        private static async IAsyncEnumerable <object> Work(List <Tuple <string, FileTreeNode> > items, int bufferSize)
        {
            await using var memoryStream = new MemoryStream();
            var writer = WriterFactory.Open(memoryStream, ArchiveType.Zip, CompressionType.Deflate);

            for (var i = 0; i < items.Count; i++)
            {
                var(name, node)            = items[i];
                await using var readStream = node.OpenRead();
                writer.Write(name, readStream);
                if (i == items.Count - 1)
                {
                    writer.Dispose();
                }
                if (memoryStream.Position < bufferSize)
                {
                    continue;
                }
                int pos;
                for (pos = 0; pos <= memoryStream.Position - bufferSize; pos += bufferSize)
                {
                    var data = new ReadOnlySequence <byte>(memoryStream.GetBuffer(), pos, bufferSize);
                    yield return(data);
                }

                if (memoryStream.Position > pos)
                {
                    Array.Copy(memoryStream.GetBuffer(), pos, memoryStream.GetBuffer(), 0, memoryStream.Position - pos);
                }

                memoryStream.Seek(memoryStream.Position - pos, SeekOrigin.Begin);
            }

            yield return(new ReadOnlySequence <byte>(memoryStream.GetBuffer(), 0, (int)memoryStream.Position));
        }
Beispiel #8
0
        public static void Compress(
            string rootDir,
            IEnumerable <FileInfo> files,
            Stream outStream,
            ArchiveType archiveType,
            CompressionType compressionType,
            ProgressStreamReportDelegate progressCallback)
        {
            char[] dirSeparators = { '\\', '/' };

            if (progressCallback != null)
            {
                outStream = new ProgressStream(outStream);
                ((ProgressStream)outStream).BytesWritten += progressCallback;
            }

            int rootDirLen = rootDir.Length + (dirSeparators.Contains(rootDir.Last()) ? 0 : 1);

            using (var writer = WriterFactory.Open(outStream, archiveType, new WriterOptions(compressionType)))
            {
                foreach (FileInfo f in files)
                {
                    writer.Write(f.FullName.Substring(rootDirLen), f);
                }
            }

            if (progressCallback != null)
            {
                ((ProgressStream)outStream).BytesWritten -= progressCallback;
            }
        }
Beispiel #9
0
        public void PacketsDocumentation()
        {
            var builder = new StringBuilder();

            builder.AppendLine("# NosCore.Packets's Documentation");
            builder.AppendLine("## ClientPackets :");
            foreach (IGrouping <string, Type> packetGroup in GetPacketsWithinNamespace("ClientPackets"))
            {
                builder.AppendLine();
                builder.AppendLine($"### {packetGroup.Key}");
                foreach (Type packet in GetPackets(packetGroup))
                {
                    builder.AppendLine($"- [{packet.GetCustomAttribute<PacketHeaderAttribute>()!.Identification}](src/NosCore.Packets/ClientPackets/{packetGroup.Key}/{packet}.cs)");
                }
            }

            builder.AppendLine();
            builder.AppendLine("## ServerPackets :");
            foreach (IGrouping <string, Type> packetGroup in GetPacketsWithinNamespace("ServerPackets"))
            {
                builder.AppendLine();
                builder.AppendLine($"### {packetGroup.Key}");
                foreach (Type packet in GetPackets(packetGroup))
                {
                    builder.AppendLine($"- [{packet.GetCustomAttribute<PacketHeaderAttribute>()!.Identification}](src/NosCore.Packets/ServerPackets/{packetGroup.Key}/{packet}.cs)");
                }
            }

            Approvals.Verify(WriterFactory.CreateTextWriter(builder.ToString(), "md"));
        }
		/// <summary>
		/// 实现IHttpModule的Init接口
		/// </summary>
		/// <param name="app"></param>
		public void Init(HttpApplication app)
		{
            // 确保配置文件已读取
            WriterFactory.Init();


            // 检查配置文件,是否启用 <Type DataType="ClownFish.Log.Model.PerformanceInfo, ClownFish.Log"
            if( WriterFactory.Config.Types.FirstOrDefault(x => x.Type == typeof(PerformanceInfo)) == null ) {
                throw new System.Configuration.ConfigurationErrorsException(
                        "启用 PerformanceModule 时,必需在 ClownFish.Log.config 的<Types>节点中注册 PerformanceInfo 数据类型。");
            }

            app.BeginRequest += App_BeginRequest;

            if( WriterFactory.Config.Performance?.HttpExecuteTimeout > 0 ) {
                // 启用 HttpHandler 性能监控
                // 这里只记录 Handler 的执行时间,排除管线过程中HttpModule的执行时间
                app.PreRequestHandlerExecute += app_PreRequestHandlerExecute;
                app.PostRequestHandlerExecute += app_PostRequestHandlerExecute;
            }

            if( WriterFactory.Config.Performance?.DbExecuteTimeout > 0 ) {
                // 启用数据库性能监控,注册扩展类型:EventManagerEventSubscriber
                bool? flag = InternalBridge.RegisterEventManagerEventSubscriber?.Invoke();

                // 说明:如果希望上面这行代码能正常工作,
                // 必须在程序初始化时调用 ClownFish.Data.Initializer.Instance.XXX 来初始化ClownFish.Data
                // 在 ClownFish.Data.Initializer 的构造方法中,会给 InternalBridge.RegisterEventManagerEventSubscriber 赋值

                // 如果是第一次调用,再注册回调委托,供 EventManagerEventSubscriber 使用
                if( flag.HasValue && flag.Value ) {
                    InternalBridge.PerformanceModuleCheckDbExecuteTime = CheckDbExecuteTime;
                }
            }
        }
Beispiel #11
0
        public void WriterFactory_MobileWriter()
        {
            Writer w = WriterFactory.Create(OutputDeviceType.Mobile);

            //assert
            Assert.IsInstanceOfType(w, typeof(MobileWriter));
        }
Beispiel #12
0
        private void Initialize(string configFileName)
        {
            try
            {
                var    config         = Config.Load(configFileName);
                string validationText = config.ValidateErrors();

                if (validationText == null)
                {
                    m_currentConfig      = config;
                    competitionCode.Text = config.CompetitionCode;

                    m_db             = new Database(m_currentConfig);
                    m_scoresRepo     = new QualificationScoresRepo(m_db);
                    m_tournamentRepo = new TournamentRepo(m_db);
                    m_Writer         = WriterFactory.GetWriter(m_currentConfig);

                    GenerateNow.Enabled     = true;
                    startPublishing.Enabled = true;
                }
                else
                {
                    MessageBox.Show($"Config file validation failed.\r\n{validationText}");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Unable to open file.\n\r{ex.ToString()}");
            }
        }
Beispiel #13
0
        static void Main(string[] args)
        {
            WriterType writeTo;

            if (!Enum.TryParse <WriterType>(ConfigurationManager.AppSettings["WriteTo"], true, out writeTo))
            {
                Console.WriteLine("Invalid WriteTo option. Program is ending");
                return;
            }
            var baseAddress = ConfigurationManager.AppSettings["HelloWorldDemoUrl"];

            if (string.IsNullOrEmpty(baseAddress))
            {
                Console.WriteLine("HelloWorldDemoUrl not found. Program is ending");
                return;
            }

            var client        = new Client(new HttpClient(), baseAddress);
            var writerFactory = new WriterFactory();
            var worker        = new Worker(client, writerFactory);

            worker.RunHelloWordAsync(writeTo).GetAwaiter().GetResult();

            Console.ReadKey();
        }
Beispiel #14
0
        public void CreateZipArchive(string filename, long files, long filesize, long chunksize, bool set_zip64, bool forward_only)
        {
            var data = new byte[chunksize];

            // Use deflate for speed
            var opts = new ZipWriterOptions(CompressionType.Deflate)
            {
                UseZip64 = set_zip64
            };

            // Use no compression to ensure we hit the limits (actually inflates a bit, but seems better than using method==Store)
            var eo = new ZipWriterEntryOptions()
            {
                DeflateCompressionLevel = Compressors.Deflate.CompressionLevel.None
            };

            using (var zip = File.OpenWrite(filename))
                using (var st = forward_only ? (Stream) new ForwardOnlyStream(zip) : zip)
                    using (var zipWriter = (ZipWriter)WriterFactory.Open(st, ArchiveType.Zip, opts))
                    {
                        for (var i = 0; i < files; i++)
                        {
                            using (var str = zipWriter.WriteToStream(i.ToString(), eo))
                            {
                                var left = filesize;
                                while (left > 0)
                                {
                                    var b = (int)Math.Min(left, data.Length);
                                    str.Write(data, 0, b);
                                    left -= b;
                                }
                            }
                        }
                    }
        }
Beispiel #15
0
        protected void Write(CompressionType compressionType, string archive, string archiveToVerifyAgainst)
        {
            using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive))) {
                WriterOptions writerOptions = new WriterOptions(compressionType)
                {
                    LeaveStreamOpen = true,
                };

                writerOptions.ArchiveEncoding.Default = Encoding.GetEncoding(866);

                using (var writer = WriterFactory.Open(stream, type, writerOptions))
                {
                    writer.WriteAll(ORIGINAL_FILES_PATH, "*", SearchOption.AllDirectories);
                }
            }
            CompareArchivesByPath(Path.Combine(SCRATCH2_FILES_PATH, archive),
                                  Path.Combine(TEST_ARCHIVES_PATH, archiveToVerifyAgainst));

            using (Stream stream = File.OpenRead(Path.Combine(SCRATCH2_FILES_PATH, archive)))
            {
                ReaderOptions readerOptions = new ReaderOptions();

                readerOptions.ArchiveEncoding.Default = Encoding.GetEncoding(866);

                using (var reader = ReaderFactory.Open(new NonDisposingStream(stream), readerOptions))
                {
                    reader.WriteAllToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions()
                    {
                        ExtractFullPath = true
                    });
                }
            }
            VerifyFiles();
        }
        public void Tar_FileName_Exactly_100_Characters()
        {
            string archive = "Tar_FileName_Exactly_100_Characters.tar";


            // create the 100 char filename
            string filename = "filename_with_exactly_100_characters_______________________________________________________________X";

            // Step 1: create a tar file containing a file with the test name
            using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive)))
                using (var writer = WriterFactory.Open(stream, ArchiveType.Tar, CompressionType.None))
                    using (Stream inputStream = new MemoryStream())
                    {
                        StreamWriter sw = new StreamWriter(inputStream);
                        sw.Write("dummy filecontent");
                        sw.Flush();

                        inputStream.Position = 0;
                        writer.Write(filename, inputStream, null);
                    }

            // Step 2: check if the written tar file can be read correctly
            string unmodified = Path.Combine(SCRATCH2_FILES_PATH, archive);

            using (var archive2 = TarArchive.Open(unmodified))
            {
                Assert.Equal(1, archive2.Entries.Count);
                Assert.Contains(filename, archive2.Entries.Select(entry => entry.Key));

                foreach (var entry in archive2.Entries)
                {
                    Assert.Equal("dummy filecontent", new StreamReader(entry.OpenEntryStream()).ReadLine());
                }
            }
        }
Beispiel #17
0
 public static void Verify(IExecutableQuery query)
 {
     Verify(
         WriterFactory.CreateTextWriter(query.GetQuery()),
         GetDefaultNamer(),
         new ExecutableQueryFailure(query, GetReporter()));
 }
Beispiel #18
0
        public static void SendData([NotNull] string name, [NotNull] object obj)
        {
            using (var memory = new MemoryStream()) {
                using (var writer = WriterFactory.Open(memory, ArchiveType.Zip, CompressionType.Deflate)) {
                    try {
                        writer.WriteString(name, JsonConvert.SerializeObject(obj));
                    } catch (Exception e) {
                        Logging.Warning($"Can’t attach {obj}: " + e);
                    }
                }

                var data = memory.ToArray();
                if (data.Length > 20000000)
                {
                    File.WriteAllBytes(FilesStorage.Instance.GetTemporaryFilename("Data.zip"), data);
                    throw new Exception("Size limit exceeded");
                }

#if DEBUG
                File.WriteAllBytes(FilesStorage.Instance.GetTemporaryFilename("Data.zip"), data);
#endif

                InternalUtils.SendData(memory.ToArray(), $@"Name: {GetUserName()}
Operating system: {GetWindowsName()}
App version: {BuildInformation.AppVersion}", CmApiProvider.UserAgent);
            }
        }
        public void TestSharpCompressWithEmptyStream()
        {
            MemoryStream stream = new NonSeekableMemoryStream();

            using (IWriter zipWriter = WriterFactory.Open(stream, ArchiveType.Zip, CompressionType.Deflate))
            {
                zipWriter.Write("foo.txt", new MemoryStream(new byte[0]));
                zipWriter.Write("foo2.txt", new MemoryStream(new byte[10]));
            }

            stream = new MemoryStream(stream.ToArray());
            File.WriteAllBytes("foo.zip", stream.ToArray());

            using (var zipArchive = ZipArchive.Open(stream))
            {
                foreach (var entry in zipArchive.Entries)
                {
                    using (var entryStream = entry.OpenEntryStream())
                    {
                        MemoryStream tempStream = new MemoryStream();
                        const int    bufSize    = 0x1000;
                        byte[]       buf        = new byte[bufSize];
                        int          bytesRead  = 0;
                        while ((bytesRead = entryStream.Read(buf, 0, bufSize)) > 0)
                        {
                            tempStream.Write(buf, 0, bytesRead);
                        }
                    }
                }
            }
        }
Beispiel #20
0
        private static void CreateUpdatePackage(string artifactsDir,
                                                string outputDir,
                                                string productVersion)
        {
            var updateDir = Path.Combine(outputDir, "Update-ToZip");

            Directory.CreateDirectory(updateDir);

            FileHelper.CopyDirectory(artifactsDir, updateDir);

            using (var stream = File.OpenWrite(Path.Combine(outputDir, productVersion + ".tar.lz")))
            {
                var writerOptions = new WriterOptions(CompressionType.LZip)
                {
                    LeaveStreamOpen = false,
                    ArchiveEncoding = { Default = Encoding.GetEncoding(866) },
                };

                using (var writer = WriterFactory.Open(stream, ArchiveType.Tar, writerOptions))
                {
                    writer.WriteAll(updateDir, "*", SearchOption.AllDirectories);
                }
            }

            Directory.Delete(updateDir, true);
        }
Beispiel #21
0
        public ActionResult WriterArchive(RenderModel model, int id, string name, FILTER filter = FILTER.Recommendations, int page = 1)
        {
            IPublishedContent recommendationsRespository = Helper.TypedContentAtRoot().FirstOrDefault()
                                                           .FirstChild(x => x.DocumentTypeAlias == Constants.NodeAlias.RECOMMENDATIONS_REPOSITORY);

            IPublishedContent articleRespository = Helper.TypedContentAtRoot().FirstOrDefault()
                                                   .FirstChild(x => x.DocumentTypeAlias == Constants.NodeAlias.ARTICLE_REPOSISTORY);

            IPublishedContent locationRespository = Helper.TypedContentAtRoot().FirstOrDefault()
                                                    .FirstChild(x => x.DocumentTypeAlias == Constants.NodeAlias.LOCATION_REPOSITORY);

            WriterModel writerModel = WriterFactory
                                      .Create(model.Content, recommendationsRespository, articleRespository, locationRespository, Site.IsEnglish, false, Services.ContentService, filter, page);

            string expectedName = writerModel.Name.ToSeoUrl();

            string actualName = (name ?? "").ToLower();

            // permanently redirect to the correct URL
            if (expectedName != actualName)
            {
                return(RedirectToActionPermanent(Constants.Controllers.WriterArchive.NAME,
                                                 Constants.Controllers.WriterArchive.Actions.INDEX,
                                                 new
                {
                    id = writerModel.Id,
                    name = expectedName,
                    filter
                }));
            }

            return(CurrentTemplate(writerModel));
        }
Beispiel #22
0
        public void WriterFactory_DatabaseWriter()
        {
            Writer w = WriterFactory.Create(OutputDeviceType.Database);

            //assert
            Assert.IsInstanceOfType(w, typeof(DatabaseWriter));
        }
        protected void Write(CompressionType compressionType, string archive, string archiveToVerifyAgainst)
        {
            ResetScratch();
            using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive)))
            {
                using (var writer = WriterFactory.Open(new NonDisposingStream(stream), type, new WriterOptions(compressionType)
                {
                    LeaveStreamOpen = true
                }))
                {
                    writer.WriteAll(ORIGINAL_FILES_PATH, "*", SearchOption.AllDirectories);
                }
                if (!stream.CanWrite)
                {
                    throw new InvalidOperationException();
                }
            }
            CompareArchivesByPath(Path.Combine(SCRATCH2_FILES_PATH, archive),
                                  Path.Combine(TEST_ARCHIVES_PATH, archiveToVerifyAgainst));

            using (Stream stream = File.OpenRead(Path.Combine(SCRATCH2_FILES_PATH, archive)))
                using (var reader = ReaderFactory.Open(new NonDisposingStream(stream)))
                {
                    reader.WriteAllToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions()
                    {
                        ExtractFullPath = true
                    });
                }
            VerifyFiles();
        }
Beispiel #24
0
        public async Task <DeviceSource> ConnectAsync(string port, UInt16 universe)
        {
            try
            {
                var writer = await WriterFactory.CreateDmxWriterAsync(port);

                if (!Writers.Values.Any(x => x.UniverseID == universe))
                {
                    // start listening on a new universe
                    SacnReceiver.JoinMulticastGroup(universe);
                }
                Writers.Add(port, new WriterEntry {
                    Writer = writer, UniverseID = universe
                });
            }
            catch (Exception ex)
            {
                await Console.Error.WriteAsync(ex.ToString());

                throw new ExecutionError(ex.Message);
            }
            return(new DeviceSource
            {
                Id = port,
                Status = DeviceStatus.InUse,
                Universe = universe
            });
        }
Beispiel #25
0
        public static string BuildSamplePackage(string name, string version, bool excludeNonNativePlatformScripts = true)
        {
            var sourceDirectory = TestEnvironment.GetTestPath("Fixtures", "Deployment", "Packages", name);

            var output = Path.Combine(Path.GetTempPath(), "CalamariTestPackages");
            Directory.CreateDirectory(output);

            var outputTarFilename = Path.Combine(output, name + "." + version + ".tar.gz");
            if (File.Exists(outputTarFilename))
                File.Delete(outputTarFilename);

            using (Stream stream = File.OpenWrite(outputTarFilename))
            using (var writer = WriterFactory.Open(stream, ArchiveType.Tar, new WriterOptions(CompressionType.GZip) {LeaveStreamOpen = false}))
            {
                var isRunningOnWindows = CalamariEnvironment.IsRunningOnWindows;

                var files = Directory.EnumerateFiles(sourceDirectory, "*.*", SearchOption.AllDirectories)
                    .Select(f => new FileInfo(f))
                    .Where(f => isRunningOnWindows
                        ? !IsNixNuspecFile(f)
                        : !IsNotANuspecFile(f) || IsNixNuspecFile(f));

                if (excludeNonNativePlatformScripts)
                    files = files.Where(f => isRunningOnWindows ? f.Extension != ".sh" : f.Extension != ".ps1");

                foreach(var file in files)
                    writer.Write(GetFilePathRelativeToRoot(sourceDirectory, file), file);
            }
            
            return outputTarFilename;

            bool IsNixNuspecFile(FileInfo f) => f.Name.EndsWith(".Nix.nuspec");
            bool IsNotANuspecFile(FileInfo f) => f.Name.EndsWith(".nuspec");
        }
        private void OnExportConfig(object sender, RoutedEventArgs e)
        {
            SaveFileDialog diag = new SaveFileDialog();

            diag.Title      = "Enter zip file";
            diag.Filter     = "Zip files|*.zip";
            diag.DefaultExt = "zip";
            if (diag.ShowDialog() == true)
            {
                System.IO.DirectoryInfo dirInfo   = new System.IO.DirectoryInfo(Configuration.GetDataPath());
                System.IO.FileInfo[]    filesList = dirInfo.GetFiles();

                using (var zipWriter = WriterFactory.Open(System.IO.File.OpenWrite(diag.FileName), ArchiveType.Zip, CompressionType.Deflate))
                {
                    foreach (var filePath in filesList)
                    {
                        if (filePath.Name.ToUpperInvariant().EndsWith(".LOG") || filePath.Name.ToUpperInvariant().EndsWith(".PMO") || filePath.Name.ToUpperInvariant() == "CONFIG.DAT" || filePath.Name.ToUpperInvariant().EndsWith(".aio"))
                        {
                            try
                            {
                                zipWriter.Write(filePath.Name, filePath);
                            }
                            catch { }
                        }
                    }
                }

                MessageBox.Show("Configuration Export complete.");
            }
        }
Beispiel #27
0
        /// <summary>
        /// 获取要显示的日志根目录,
        /// 重写这个方法可修改默认的根目录(FileWriter指定的目录)。
        /// </summary>
        /// <returns></returns>
        public virtual string GetLogRootDirectory()
        {
            WriterFactory.Init();

            WriterSection config = (from x in WriterFactory.Config.Writers
                                    where x.Type.StartsWith(typeof(FileWriter).FullName + ",")
                                    select x).FirstOrDefault();

            if (config == null)
            {
                WriteMessage("ClownFish.Log.config中没有配置FileWriter");
                return(null);
            }

            string path = config.GetOptionValue("RootDirectory");

            if (string.IsNullOrEmpty(path))
            {
                WriteMessage("ClownFish.Log.config中,没有为FileWriter指定RootDirectory属性。");
                return(null);
            }

            path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
            if (Directory.Exists(path) == false)
            {
                WriteMessage("ClownFish.Log.config中,为FileWriter指定RootDirectory目录不存在。");
                return(null);
            }

            return(path);
        }
Beispiel #28
0
        public void TestSharpCompressWithEmptyStream()
        {
            ResetScratch();

            MemoryStream stream = new NonSeekableMemoryStream();

            using (IWriter zipWriter = WriterFactory.Open(stream, ArchiveType.Zip, CompressionType.Deflate))
            {
                zipWriter.Write("foo.txt", new MemoryStream(new byte[0]));
                zipWriter.Write("foo2.txt", new MemoryStream(new byte[10]));
            }

            stream = new MemoryStream(stream.ToArray());
            File.WriteAllBytes(Path.Combine(SCRATCH_FILES_PATH, "foo.zip"), stream.ToArray());

            using (IReader zipReader = ZipReader.Open(stream))
            {
                while (zipReader.MoveToNextEntry())
                {
                    using (EntryStream entry = zipReader.OpenEntryStream())
                    {
                        MemoryStream tempStream = new MemoryStream();
                        const int    bufSize    = 0x1000;
                        byte[]       buf        = new byte[bufSize];
                        int          bytesRead  = 0;
                        while ((bytesRead = entry.Read(buf, 0, bufSize)) > 0)
                        {
                            tempStream.Write(buf, 0, bytesRead);
                        }
                    }
                }
            }
        }
Beispiel #29
0
        /// <summary>
        /// 以同步方式把消息写入日志
        /// 如果需要写入到指定的持久化方式,可以直接调用相应的 Writter ,就不需要调用这个方法。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="info"></param>
        public static void SyncWrite <T>(T info) where T : BaseInfo
        {
            // 触发日志的配置检查
            WriterFactory.Init();

            // 如果禁用日志写入就直接返回
            if (WriterFactory.Config.Enable == false)
            {
                return;
            }

            // 忽略特定的对象类型
            if (s_filter.Instance.IgnoreWrite(info))
            {
                return;
            }


            // 获取写日志的实例,注意:允许一个类型配置多个写入方式
            ILogWriter[] writers = WriterFactory.CreateWriters(typeof(T));

            // 如果类型没有配置日志序列化器,就忽略
            if (writers == null || writers.Length == 0)
            {
                return;
            }


            foreach (var writer in writers)
            {
                writer.Write(info);
            }
        }
Beispiel #30
0
        /**
         * Compress the contents of given directory using Tar and Gzip to an in-memory byte array.
         *
         * @param sourceDirectory  the source directory.
         * @param pathPrefix       a path to be prepended to every file name in the .tar.gz output, or {@code null} if no prefix is required.
         * @param chaincodeMetaInf
         * @return the compressed directory contents.
         * @throws IOException
         */
        public static byte[] GenerateTarGz(string sourceDirectory, string pathPrefix, string chaincodeMetaInf)
        {
            logger.Trace($"generateTarGz: sourceDirectory: {sourceDirectory}, pathPrefix: {pathPrefix}, chaincodeMetaInf: {chaincodeMetaInf}");
            string sourcePath = sourceDirectory;

            string[] sfiles = Directory.GetFiles(sourceDirectory, "*", SearchOption.AllDirectories);
            string[] mfiles = chaincodeMetaInf != null?Directory.GetFiles(chaincodeMetaInf, "*", SearchOption.AllDirectories) : new string[0];

            using (MemoryStream bos = new MemoryStream())
            {
                using (var writer = WriterFactory.Open(bos, ArchiveType.Tar, CompressionType.GZip))
                {
                    foreach (string s in sfiles)
                    {
                        writer.Write(Path.Combine(pathPrefix ?? "", s.Substring(sourcePath.Length + 1)).Replace("\\", "/"), s);
                    }
                    if (chaincodeMetaInf != null)
                    {
                        foreach (string s in mfiles)
                        {
                            writer.Write(Path.Combine("META-INF", s.Substring(chaincodeMetaInf.Length + 1)).Replace("\\", "/"), s);
                        }
                    }

                    bos.Flush();
                }

                return(bos.ToArray());
            }
        }
Beispiel #31
0
 public static void RegisterWriter(string scheme, WriterFactory factory)
     {
     writers.Add(scheme, factory);
 }