Help me to implement code compress file .

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vampire1986
    New Member
    • Jan 2008
    • 2

    #1

    Help me to implement code compress file .

    Hi all. I have project compress file using C#. I'm using name space IO.Compression and i saw this code on Internet but it is compress to file bigger than source file. Can you help me, please.Thanks for your help.
    Code:
    public void CompressFile(string inputFileName, string outputFileName)
    {
    byte[] buffer = new byte[4096];
    if (File.Exists(inputFileName))
    {
    using (FileStream inputFile = File.Open(inputFileName, FileMode.Open), outputFile = File.Create(outputFileName))
    {
    
    using (GZipStream gzip = new GZipStream(outputFile, CompressionMode.Compress))
    {
    int n = 0;
    while ((n = inputFile.Read(buffer, 0, buffer.Length)) != 0)
    {
    gzip.Write(buffer, 0, n);
    }
    }
    }
    }
    }
    
    
    public void UncompressFile(string inputFileName, string outputFileName)
    {
    byte[] buffer = new byte[4096];
    if (File.Exists(inputFileName))
    {
    using (FileStream intputFile = File.Open(inputFileName, FileMode.Open), outputFile = File.Create(outputFileName))
    {
    using (GZipStream gzip = new GZipStream(intputFile, CompressionMode.Decompress))
    {
    int n;
    while ((n = gzip.Read(buffer, 0, buffer.Length)) != 0)
    {
    outputFile.Write(buffer, 0, n);
    }
    }
    }
    }
    }
Working...