2

I found this method for compressing a single file to a single zip file using SharpZipLib.

private static void CompressFile(string inputPath, string outputPath)
{
    FileInfo outFileInfo = new FileInfo(outputPath);
    FileInfo inFileInfo = new FileInfo(inputPath);

    // Create the output directory if it does not exist
    if (!Directory.Exists(outFileInfo.Directory.FullName))
    {
        Directory.CreateDirectory(outFileInfo.Directory.FullName);
    }

    // Compress
    using (FileStream fsOut = File.Create(outputPath))
    {
        using (ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipStream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(fsOut))
        {
            zipStream.SetLevel(3);

            ICSharpCode.SharpZipLib.Zip.ZipEntry newEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(inFileInfo.Name);
            newEntry.DateTime = DateTime.UtcNow;
            zipStream.PutNextEntry(newEntry);

            byte[] buffer = new byte[4096];
            using (FileStream streamReader = File.OpenRead(inputPath))
            {
                ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(streamReader, zipStream, buffer);
            }

            zipStream.CloseEntry();
            zipStream.IsStreamOwner = true;
            zipStream.Close();
        }
    }
}

First of all, tell me is this method correct?
Secondly, how do I do compress a single file to a single zip file with FastZip?
In this tutorial, they told how to zip a directory - Not a single file > SharpZipLib-wiki-FastZip

white-wolf97
  • 106
  • 1
  • 3
  • 12
SilverLight
  • 19,668
  • 65
  • 192
  • 300

0 Answers0