Code To compress file upto nerly 97%

abelLazm 0 Tallied Votes 847 Views Share

Just came across a code of compressing file thought to share :). Code for text file only can be changed for other filetypes....It reduces nearly 97% of the file size ...

OpenFileDialog opd = new OpenFileDialog();
FolderBrowserDialog fbd = new FolderBrowserDialog();
textBox1.Text="Path + File To compress";//open file by open file dialog
textBox2.Text="Path of folder where to save the compressed file";\\selected through FolderBrowserDialog
textBox3.Text="Name of the resulting Compressed file";

textBox4.Text="Name of the resulting DeCompressed file";
textBox5.Text="Path + File To decompress";//open file by open file dialog;
textBox6.Text="Path of folder where to save the decompressed file";\\selected through FolderBrowserDialog

Refrence: The Code Project

using System.IO;
using System.IO.Compression;
//compress file
 public void Compress(string filepath)
        {
 FileStream compress = new FileStream(fbd.SelectedPath + "\\" + textBox3.Text + ".txt", FileMode.Create, FileAccess.Write);
                FileStream decom = new FileStream(filepath, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(decom);
                string readDecom = sr.ReadToEnd();

                GZipStream zip = new GZipStream(compress, CompressionMode.Compress);

                StreamWriter sw = new StreamWriter(zip);

                sw.WriteLine(readDecom);
                sw.Flush();
                zip.Close();
                compress.Close();
}
//Decompress file to retrieve data
  public void DeCompress(string fileDeComp)
        {
            FileStream compfile = new FileStream(opd.FileName, FileMode.Open, FileAccess.Read);
            FileStream wrt_to_decomp = new FileStream(fbd.SelectedPath+"\\"+textBox4.Text+".txt",FileMode.Create,FileAccess.Write);
            GZipStream zip = new GZipStream(compfile, CompressionMode.Decompress);
            StreamReader read_comp = new StreamReader(zip);

            StreamWriter sw = new StreamWriter(wrt_to_decomp);
            sw.WriteLine(read_comp.ReadToEnd());
            textBox2.Text = read_comp.ReadToEnd();
            sw.Flush();
            compfile.Close();
            zip.Close();

        }
Momerath 1,327 Nearly a Senior Poster Featured Poster

Problems with this code:

It makes use of values that aren't declared or passed as values (the various textbox values).

There is no error checking at all.

It has bugs in it. I'll let you test your debugging skills. Just a quick glance over the code shows at least seven.

abelLazm 183 Postaholic

:)... This is not the complete project it is working fine when written in project... this is only a small part of a compressing application

ddanbe 2,724 Professional Procrastinator Featured Poster

Hmmm, I wonder ...
So if I have a textfile consisting of 26 characters, the alphabet for example, I could reduce its size by 97% of 26, being 25.22???
So I could zip all the information of the alphabet in less than 1 char?

Momerath 1,327 Nearly a Senior Poster Featured Poster

That may be true, but it still has bugs in it, so it's not 'working fine'.

abelLazm 183 Postaholic

It is working fine with me... I have added some details You can check it now

Tellalca 19 Posting Whiz in Training

Hmmm, I wonder ...
So if I have a textfile consisting of 26 characters, the alphabet for example, I could reduce its size by 97% of 26, being 25.22???
So I could zip all the information of the alphabet in less than 1 char?

It says "nearly upto". So it does not have to compress all files %97, it may and it may not.

ddanbe 2,724 Professional Procrastinator Featured Poster

@Tellalca
Yes I'm aware of the fact that a pictre with almost nothing else but blue sky can be compressed more than a picture taken on an average market place.
But I still find a compression rate of 97% very high.

CsharpChico 1 Junior Poster in Training

I have used this code in the past i have the complete source which i found on either codeproject or vckicks. I have used this method to create my own image format. here is how to load and save as image using this method.
here is how to load the image

string lfile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\StrGraphic.lib";
                       FileStream compfile = new FileStream(FileName, FileMode.Open, FileAccess.Read);
                       FileStream wrt_to_decomp = new FileStream(lfile, FileMode.Create, FileAccess.Write);                       
                       StreamReader read_comp = new StreamReader(new GZipStream(compfile, CompressionMode.Decompress));
                       StreamWriter sw = new StreamWriter(wrt_to_decomp, Encoding.Unicode);
                       sw.WriteLine(read_comp.ReadToEnd());                       
                       sw.Flush();
                       compfile.Close();                       
                       sw.Close();
                       StreamReader sr = new StreamReader(lfile);
                       LoadedImage = new Bitmap(Image.FromStream(new MemoryStream(Convert.FromBase64String(sr.ReadToEnd()))));

Note: When opening the image file use a Stream to store the file as a byte[] array so that it can be used to save the image.
and FileName from line 2 should be replaced with the name of the image file you are opening.

Here is how to save the image
Note: When saving the image Filename from line 1 should be changed to name you are saving the image as.

private void SaveCifImage(string FileName, byte[] Data)
        {
            FileStream compress = new FileStream(FileName, FileMode.Create, FileAccess.Write);
            StreamWriter strw = new StreamWriter(new GZipStream(compress, CompressionMode.Compress));
            strw.WriteLine(Convert.ToBase64String(Data));
            strw.Flush();            
            compress.Close(); 
        }

Note: when using png format the compressed format will be larger than the original image

abelLazm commented: Nice work +1
skatamatic 371 Practically a Posting Shark

I was hoping to see an actual compression algorithm, not an implementation of one you didn't write...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.