Exemplo n.º 1
0
        private static void CompressFiles(IEnumerable <Node> files, int compressorVersion)
        {
            var compressorParameters = new CompressorParameters
            {
                Endianness = 0x00,
                Version    = (byte)compressorVersion,
            };

            Parallel.ForEach(files, node =>
            {
                var parFile = node.GetFormatAs <ParFile>();
                if (parFile == null || !parFile.CanBeCompressed || compressorVersion == 0x00 ||
                    parFile.Stream.Length == 0)
                {
                    return;
                }

                FileCompressing?.Invoke(node);
                var compressed =
                    (ParFile)ConvertFormat.With <Compressor, CompressorParameters>(compressorParameters, parFile);

                long diff = parFile.Stream.Length - compressed.Stream.Length;
                if (diff >= 0 && (parFile.Stream.Length < 2048 || diff >= 2048))
                {
                    node.ChangeFormat(compressed);
                }

                FileCompressed?.Invoke(node);
            });
        }
Exemplo n.º 2
0
        public void Compress(FileStream fs)
        {
            //build folders list
            var folders = new List <string>();

            var seq = from fd in m_files
                      where fd.DestFolder != null
                      select fd.DestFolder;


            folders.AddRange(seq.Distinct(StringComparer.OrdinalIgnoreCase));

            using (var gzs = new GZipStream(fs, CompressionMode.Compress))
            {
                var writer = new RawDataWriter(gzs, Encoding.UTF8);

                writer.Write(Signature);
                writer.Write(folders.Count);

                foreach (string folder in folders)
                {
                    writer.Write(folder);
                }

                writer.Write(m_files.Count);

                foreach (FileData fd in m_files)
                {
                    writer.Write(fd.FileName);

                    if (fd.DestFolder == null)
                    {
                        writer.Write(NDX_CUR_FOLDER);
                    }
                    else
                    {
                        string dir = fd.DestFolder;
                        int    ndx = folders.FindIndex(x => string.Compare(dir, x, true) == 0);

                        Assert(ndx >= 0);

                        writer.Write(ndx);
                    }

                    using (FileStream fStream = File.OpenRead(fd.FilePath))
                    {
                        writer.Write(fStream.Length);
                        fStream.CopyTo(gzs);
                    }

                    FileCompressed?.Invoke(Path.Combine(fd.DestFolder ?? ".\\", fd.FileName));
                }
            }
        }