Thursday, October 21, 2010

Code For COMPRESS & DECOMPRESS

COMPRESS & DECOMPRESS

protected void Button1_Click(object sender, EventArgs e)
    {
        FileStream fs = new FileStream("C:/Documents and Settings/subbalakshmi.BSGROUP/Desktop/Test.doc", FileMode.Open);
        byte[] input=new byte[fs.Length];


        //FileStream fs = new FileStream("Test.doc", FileMode.Open);
        //byte[] input = new byte[fs.Length];
        fs.Read(input, 0, input.Length);
        fs.Close();

        FileStream fsOutput = new FileStream("C:/Documents and Settings/subbalakshmi.BSGROUP/Desktop/Test.rar",
                                             FileMode.Create,
                                             FileAccess.Write);
        GZipStream zip = new GZipStream(fsOutput, CompressionMode.Compress);

        zip.Write(input, 0, input.Length);
        zip.Close();
        fsOutput.Close();

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        FileStream fs = new FileStream("C:/Documents and Settings/subbalakshmi.BSGROUP/Desktop/Test.rar", FileMode.Open);
        FileStream fsOutput = new FileStream("C:/Documents and Settings/subbalakshmi.BSGROUP/Desktop/Test.doc",
                                             FileMode.Create,
                                             FileAccess.Write);
        GZipStream zip = new GZipStream(fs, CompressionMode.Decompress, true);

        byte[] buffer = new byte[4096];
        int bytesRead;
        bool continueLoop = true;
        while (continueLoop)
        {
            bytesRead = zip.Read(buffer, 0, buffer.Length);
            if (bytesRead == 0)
                break;
            fsOutput.Write(buffer, 0, bytesRead);
        }
        zip.Close();
        fsOutput.Close();
        fs.Close();

    }

No comments:

Post a Comment