Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
tittle says every thing, I tried to compress and increase the size of a file with code blow but it made it's size larger (500mb to 950mb !!!!)
Code :

C#
     byte[] buffer = new byte[1];
                    sread = new System.IO.FileStream(@"C:\Users\mm\Desktop\code.txt", System.IO.FileMode.Open, System.IO.FileAccess.Read);
                    swrite = new System.IO.FileStream(@"C:\Users\mm\Desktop\0.txt", System.IO.FileMode.Create, System.IO.FileAccess.Write);

                    System.IO.Compression.GZipStream g = new     System.IO.Compression.GZipStream(swrite, System.IO.Compression.CompressionMode.Compress,true);

br = new System.IO.BinaryReader(sread);
bw = new System.IO.BinaryWriter(swrite);
while (true)
{
    try
    {
        buffer= br.ReadBytes(1);
        g.Write(buffer, 0, 1);
    }
    catch { break; }
}
sread.Close();
swrite.Close();
br.Close();
bw.Close();




How can I make it right??
Thanks.
Posted

I suppose you are not properly using the GZipStream class. Why don't you follow the safe path (namely the MSDN code sample you may find here[^])?
 
Share this answer
 
v2
I have just run some tests and suspect that the problem is due to you using a single byte for each read and write operation. Try using a larger buffer and see what results you get.
 
Share this answer
 
Comments
mehdi_k 29-Nov-11 13:23pm    
I'v used a 128 byte array , but it makes it larger like past!!
Please help me about it.
Richard MacCutchan 30-Nov-11 3:12am    
Try something larger like 4096; I used 1024 in my test and it gave much better compression than using 1 byte.
mehdi_k 30-Nov-11 7:19am    
I used 1024 and and even more but again it makes the file larger. I try to save the compressed file as the same extension with the source file (eg : Flv to flv or txt to txt), does the compressed file have to save with especial extension?
I dont know waht's the problem.
Richard MacCutchan 30-Nov-11 9:01am    
I just retested this and got the following results:
Original size: 28762
Buffer size: 1, Zipped size: 50,172
Buffer size: 1024, Zipped size: 1010
Buffer size: 4096, Zipped size: 980

Are you sure you changed the sizes in your ReadBytes() and Write() calls when you changed the buffer size?
mehdi_k 30-Nov-11 11:04am    
Yes I am, your results are no match with mine!! in any case compressed file has the same size!, I'm just stuck. :(

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