Click here to Skip to main content
15,030,320 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)
   
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.
loai_maane 1-Jul-13 15:00pm
   
yes it is work thnx a lot
OriginalGriff 1-Jul-13 15:11pm
   
You're welcome!
loai_maane 2-Jul-13 0:55am
   
string zipfile="E:\\"+t+".accdb.gz";
OriginalGriff 2-Jul-13 3:49am
   
A suggestion, if you don't mind?

Calling your files 1-7-2013 and so forth may not be the best idea: if instead to use the ISO format yyyy-MM-dd then they sort very simply, and it is easy to find both the oldest and the newest. If you add a different database to this next year, it would have been helpful to include the original name of the DB in the backup file name as well. So personally, I would replace your code:
string t = DateTime.Now.Day + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year;
string zipFilename = "E:\\" + t + "accdb.gz";
With
string zipFilename = "E:\\" + Path.GetFileNameWithoutExtension(fileToBeCompressed) +
DateTime.Now.ToString(" yyyy-MM-dd") + Path.GetExtension(fileToBeCompressed) + ".gz";
What that generates is a sequence of files called:
access 2013-07-01.accdb.gz
access 2013-07-02.accdb.gz
...
Which might be easier for you to manage in future.
Just a thought - but it's the way I'd do it.
loai_maane 2-Jul-13 4:03am
   
you are right OriginalGriff. thanx a lot i will use your idea
OriginalGriff 2-Jul-13 4:12am
   
You're welcome!

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