We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Code To compress file upto nerly 97%

0
By Abel Lazm on Apr 9th, 2011 3:43 pm

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();

        }

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.

Momerath
Senior Poster
3,726 posts since Aug 2010
Reputation Points: 1,322
Solved Threads: 624
Skill Endorsements: 12

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

abelLazm
Postaholic
2,113 posts since Feb 2011
Reputation Points: 219
Solved Threads: 124
Skill Endorsements: 1

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?

ddanbe
Industrious Poster
4,277 posts since Oct 2008
Reputation Points: 2,121
Solved Threads: 720
Skill Endorsements: 26

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

Momerath
Senior Poster
3,726 posts since Aug 2010
Reputation Points: 1,322
Solved Threads: 624
Skill Endorsements: 12

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

abelLazm
Postaholic
2,113 posts since Feb 2011
Reputation Points: 219
Solved Threads: 124
Skill Endorsements: 1

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.

Tellalca
Posting Whiz in Training
209 posts since Mar 2010
Reputation Points: 29
Solved Threads: 24
Skill Endorsements: 0

@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.

ddanbe
Industrious Poster
4,277 posts since Oct 2008
Reputation Points: 2,121
Solved Threads: 720
Skill Endorsements: 26

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

CsharpChico
Junior Poster in Training
72 posts since May 2010
Reputation Points: 12
Solved Threads: 8
Skill Endorsements: 0

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

skatamatic
Posting Shark
986 posts since Nov 2007
Reputation Points: 403
Solved Threads: 132
Skill Endorsements: 1

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0878 seconds using 2.76MB