Click here to Skip to main content
15,917,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i used this code for compression,
C#
string fileToBeCompressed="E:\\access.accdb";
            string t = DateTime.Now.Day + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year;
            string zipFilename = "E:\\"+t+".zip";
            FileStream target = new FileStream(zipFilename, FileMode.Create, FileAccess.Write);
            GZipStream alg = new GZipStream(target, CompressionMode.Compress);
            byte[] data = File.ReadAllBytes(fileToBeCompressed);
            alg.Write(data, 0, data.Length);
            alg.Flush();
            target.Close();


After run this code, i checked the E drive and found the file t.zip.
but when i want to make Extract here ,there is error message come in CRC for that file
how can i solve this problem .
Posted
Updated 1-Jul-13 7:48am
v2

1 solution

GZipStream does not compress into ZIP format- it saves to GZip instead. This is openable using Winzip, but only if you change the file extension: try
C#
string zipFilename = "E:\\"+t+".accdb.gz";
instead.
(You need the file extension as well, or it doesn't decompress so well)
 
Share this answer
 
Comments
loai_maane 1-Jul-13 14:23pm    
! E:\1-7-2013.accdb.gz: Unexpected end of archive
! E:\1-7-2013.accdb.gz: CRC failed in 1-7-2013.accdb. The file is corrupt

this error come when i try to do Extract file here , the file extension well .
loai_maane 1-Jul-13 14:27pm    
open any winrar file you while see something called CRC ,The problem in this CRC
OriginalGriff 1-Jul-13 14:38pm    
No, the problem is also in your code: You are closing the target stream before you close the encryption stream:
alg.Write(data, 0, data.Length);
alg.Flush();
alg.Close();
target.Close();
Is one way to do it, but it's a much better idea to use a using statement on each stream:
string fileToBeCompressed = "E:\\access.accdb";
string t = DateTime.Now.Day + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year;
string zipFilename = "E:\\" + t + "accdb.gz";
using (FileStream target = new FileStream(zipFilename, FileMode.Create, FileAccess.Write))
{
using (GZipStream alg = new GZipStream(target, CompressionMode.Compress))
{
byte[] data = File.ReadAllBytes(fileToBeCompressed);
alg.Write(data, 0, data.Length);
}
}
That way you are guaranteed that they are flushed, closed, and disposed in the correct order.

[edit]Forgot to change your code for gz file extension...[/edit]
loai_maane 1-Jul-13 14:50pm    
sorry, another problem i try your way it is good
but when i Extract the file and open it,no data in that file (my tables not available )
OriginalGriff 1-Jul-13 14:52pm    
:laugh:
Try it with a different file: make sure that it isn't something like one of your applications is holding the file in use, or hasn't flushed it's data into it properly.
If it works with a file that you know is "safe", then you need to look elsewhere.

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