コード例 #1
0
 /// <summary>
 /// Compresses specified file and creates specified archive.
 /// </summary>
 /// <param name="fileName"></param>
 /// <param name="archiveName"></param>
 public void Compress(string fileName, string archiveName)
 {
     using (FileStream fileStream = new FileStream(fileName, FileMode.Open))
     {
         using (FileStream archiveStream = new FileStream(archiveName, FileMode.OpenOrCreate))
         {
             using (GZipStream compressStream = new GZipStream(archiveStream, CompressionMode.Compress))
             {
                 blockByBlockStreamWriter.Write(fileStream, compressStream);
             }
         }
     }
 }
コード例 #2
0
        /// <summary>
        /// Compresses specified file and creates specified archive.
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="archiveName"></param>
        public void Compress(string fileName, string archiveName)
        {
            try
            {
                using (FileStream fileStream = new FileStream(fileName, FileMode.Open))
                {
                    using (FileStream archiveStream = new FileStream(archiveName, FileMode.OpenOrCreate))
                    {
                        using (GZipStream compressStream = new GZipStream(archiveStream, CompressionMode.Compress))
                        {
                            blockByBlockStreamWriter.Write(fileStream, compressStream);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                File.Delete(archiveName);

                throw ex;
            }
        }