Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how i can compress files with minimize size by .net
im used this code
C#
// Read the file we are going to compress into a FileStream
            string filename = Program.x;

            FileStream infile = File.OpenRead(filename);
            byte[] buffer = new byte[infile.Length];
            infile.Read(buffer, 0, buffer.Length);
            infile.Close();

            // Create the output file
            FileStream outfile = File.Create(Path.ChangeExtension(filename, "zip"));

            // Compress the input stream and write it to the output FileStream
            GZipStream gzipStream = new GZipStream(outfile, CompressionMode.Compress);
            gzipStream.Write(buffer, 0, buffer.Length);
            gzipStream.Close();

but give my new file is compressed with larger than the original size of the file. Of original size is 300mb give my new file compressed is 900 mb.

There is other way?

Thanks for any help
Posted
Updated 25-Jan-11 10:46am
v3

You code works all right (of course if you take appropriate file name) and it does compress when you test it with something like a text file. Probably you tried some file which was already well compressed - GZip is not very the best at compression ratio. I confirm the code created larger file if original file was compressed with ZIP.

By the way, how about this: http://dotnetzip.codeplex.com/[^]?
 
Share this answer
 
v4
Comments
Mostafa Elsadany 25-Jan-11 17:39pm    
thanks for help
i think this is supported mobile application ?
Sergey Alexandrovich Kryukov 25-Jan-11 18:30pm    
It says "also runs on mobile devices that use the .NET Compact Framework"
Mostafa Elsadany 25-Jan-11 17:56pm    
but How can I get rid of hanging in my from ?
Sergey Alexandrovich Kryukov 25-Jan-11 18:30pm    
Sorry, don't understand. "from ?"?
Sergey Alexandrovich Kryukov 25-Jan-11 18:34pm    
You should also understand if you use already compressed file using good graphics/video compression (especially lossy), you won't gain much with any general-purpose compression (but unlikely your file will grow instead of being compressed).
If you take a look at the HowTo: for this on MSDN[^] you will see that they use a slightly different technique.

Specifically:
C#
inFile.CopyTo(gzipStream);


They don't use a buffer at all.

Why not give it a try?
 
Share this answer
 
v2
Comments
Mostafa Elsadany 25-Jan-11 17:12pm    
i try but copyto in framework 4
not in 3.5
Henry Minute 25-Jan-11 17:14pm    
You are quite right. Sorry to have wasted your time. :)
Mostafa Elsadany 26-Jan-11 13:08pm    
no no thanks for help
thanks thanks thanks for your time
We wish you more of the tender
Mostafa Elsadany 26-Jan-11 13:26pm    
there is no way to compress to folder
not file

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900