示例#1
0
        void writeTextToFile(CompressMethod compressMethod)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                compressMethod(saveFileDialog.FileName, richTextBox1.Text);
            }
        }
示例#2
0
 private static string AddExtensionIfMissing(string outputPath, CompressMethod method)
 {
     if (!outputPath.Contains(".zip") && !outputPath.Contains(".7z"))
     {
         switch (method)
         {
             case CompressMethod.SevenZip: return outputPath += ".7z";
             case CompressMethod.Zip: return outputPath += ".zip";
         }
     }
     return outputPath;
 }
示例#3
0
        /// <summary>
        /// Compresses object by using GZip or Deflate algorithm.
        /// </summary>
        /// <param name="value">The object to compress.</param>
        /// <param name="method">The algorithm to use.</param>
        /// <returns>A byte array representing compressed object.</returns>
        public static byte[] Compress(this byte[] bytes, CompressMethod method = CompressMethod.GZip)
        {
            using (var outputStream = new MemoryStream())
            {
                switch (method)
                {
                case CompressMethod.GZip: return(GZipCompress(bytes, outputStream));

                case CompressMethod.Deflate: return(DeflateCompress(bytes, outputStream));

                default: return(null);
                }
            }
        }
        public CompressContent(HttpContent content, CompressMethod compressionMethod)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            this._originalContent   = content;
            this._compressionMethod = compressionMethod;

            foreach (var header in this._originalContent.Headers)
            {
                this.Headers.TryAddWithoutValidation(header.Key, header.Value);
            }

            this.Headers.ContentEncoding.Add(this._compressionMethod.ToString().ToLowerInvariant());
        }
示例#5
0
        /// <summary>
        /// Decompresses object by using GZip or Deflate algorithm.
        /// </summary>
        /// <param name="value">The byte array representing object to decompress.</param>
        /// <param name="method">The algorithm to use.</param>
        /// <returns>A byte array representing decompressed object.</returns>
        public static byte[] Decompress(this byte[] value, CompressMethod method = CompressMethod.GZip)
        {
            if (value == null)
            {
                return(null);
            }
            using (var inputStream = new MemoryStream(value))
                using (var outputStream = new MemoryStream())
                {
                    switch (method)
                    {
                    case CompressMethod.GZip: return(GZipDecompress(inputStream, outputStream));

                    case CompressMethod.Deflate: return(DeflateDecompress(inputStream, outputStream));

                    default: return(null);
                    }
                }
        }
示例#6
0
        /// <summary>
        /// Save compress stream to file.
        /// </summary>
        /// <param name="stream">Compress stream.</param>
        /// <param name="fileName">File name.</param>
        /// <param name="compressMethod">Compress method.</param>
        /// <param name="replace">Override file.</param>
        public static void WriteCompressStreamToFile(
            this string fileName,
            byte[] stream,
            CompressMethod compressMethod = CompressMethod.Deflate,
            bool replace = true)
        {
            if (replace && File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
                using (var ms = new MemoryStream(stream))
                {
                    Stream zs;
                    switch (compressMethod)
                    {
                    case CompressMethod.Deflate:
                        zs = new DeflateStream(ms, CompressionMode.Decompress);
                        break;

                    case CompressMethod.GZip:
                        zs = new GZipStream(ms, CompressionMode.Decompress);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(compressMethod));
                    }

                    try
                    {
                        zs.CopyTo(fs);
                    }
                    finally
                    {
                        zs.Flush();
                        zs.Close();
                    }
                }
        }
示例#7
0
 public Compressor(CompressMethod method = CompressMethod.Zip, int level = 9)
 {
     this.method = method;
     this.compressorLevel = level;
 }
示例#8
0
        /// <summary>
        /// Get compress stream from file. Use DeflateStream.
        /// </summary>
        /// <param name="fileName">File name</param>
        /// <param name="compressMethod">Compression method</param>
        /// <returns>Compress stream.</returns>
        public static byte[] GetCompressStreamFromFile(
            this string fileName,
            CompressMethod compressMethod = CompressMethod.Deflate)
        {
            var        tmpFile = Path.GetTempFileName();
            FileStream fs      = null;
            Stream     zip     = null;

            try
            {
                switch (compressMethod)
                {
                case CompressMethod.Deflate:
                    zip = new DeflateStream(new FileStream(tmpFile, FileMode.Create, FileAccess.Write),
                                            CompressionMode.Compress);
                    break;

                case CompressMethod.GZip:
                    zip = new GZipStream(new FileStream(tmpFile, FileMode.Create, FileAccess.Write),
                                         CompressionMode.Compress);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(compressMethod));
                }

                fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                fs.CopyTo(zip);
            }
            catch (Exception)
            {
                if (File.Exists(tmpFile))
                {
                    File.Delete(tmpFile);
                }
                throw;
            }
            finally
            {
                zip?.Flush();
                zip?.Close();
                zip?.Dispose();
                fs?.Close();
                fs?.Dispose();
            }

            byte[] res = null;
            try
            {
                if (File.Exists(tmpFile))
                {
                    res = GetStreamFromFile(tmpFile);
                }
            }
            finally
            {
                if (File.Exists(tmpFile))
                {
                    File.Delete(tmpFile);
                }
            }

            return(res);
        }