Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
problem with compress file
this code
to compress but first problem here: I cant save with file Extension
I change to zip there is no way to compress with original Extension if doc or other
C#
private void button1_Click_1(object sender, EventArgs e)
{
     OpenFileDialog op = new OpenFileDialog();
     op.ShowDialog();
     string filename = op.FileName;//@"c:\good";// //Program.x;
     SaveFileDialog sv=new SaveFileDialog ();
     sv.ShowDialog ();
     string savefile=sv.FileName ;
     FileStream infile = File.OpenRead(filename);
     byte[] buffer = new byte[infile.Length];
     infile.Read(buffer, 0, buffer.Length);
     infile.Close();
     FileStream outfile = File.Create(Path.ChangeExtension(savefile , "zip"));
     // FileStream outfile = File.Create(savefile +".zip");
               
     GZipStream gzipStream = new GZipStream(outfile, CompressionMode.Compress, true);
     // gzipStream.CanTimeout = true;
     MessageBox.Show(gzipStream.CanTimeout.ToString());
     gzipStream.Write(buffer, 0, buffer.Length);
     gzipStream.Close();
     progressBar1.Value = 100;


second problem when decompress
private void but_Decompress_Click(object sender, EventArgs e)
{
     OpenFileDialog op = new OpenFileDialog();
     op.ShowDialog();
     string filename = op.FileName;//@"c:\good";// //Program.x;
     SaveFileDialog sv = new SaveFileDialog();
     sv.ShowDialog();
     string savefile = sv.FileName;
     // string filename = Server.MapPath("TextFile.zip"); 
     FileStream infile = File.OpenRead(filename);
     DeflateStream deflateStream = new DeflateStream(infile, CompressionMode.Decompress);
     byte[] buffer = new byte[infile.Length + 100];
     int offset = 0;
     int totalCount = 0;
     while (true) 
     { 
           int bytesRead = deflateStream.Read(buffer, offset, 100);
           if (bytesRead == 0) { break; }
           offset += bytesRead; 
           totalCount += bytesRead;
     }
     FileStream outfile = File.Create(Path.ChangeExtension(savefile , "Doc"));
     outfile.Write(buffer, 0, buffer.Length);
     outfile.Close();
}


this error
Block length does not match with its complement

and I try with change
C#
int bytesRead = deflateStream.Read(buffer, offset, 100);

to
C#
int bytesRead = deflateStream.Read(buffer, offset, buffer.Length );

I still have this error
please any help
and thanks for any help

[Modified: added pre tags...they make the world a prettier place! Also fixed the tabbing and got rid of the textspeak.]
Posted
Updated 26-Jan-11 12:48pm
v2

Hi,

I've tested your code and I changed the following to make it work for me:
When compressing you can set the Filter of the SaveFileDialog to "zip" instead of changing the extension afterwards:
C#
OpenFileDialog op = new OpenFileDialog();
op.ShowDialog();
string filename = op.FileName;//@"c:\good";// //Program.x;
SaveFileDialog sv = new SaveFileDialog();
sv.Filter = "Zip files|*.zip";
sv.ShowDialog();
string savefile = sv.FileName;
FileStream infile = File.OpenRead(filename);
byte[] buffer = new byte[infile.Length];
infile.Read(buffer, 0, buffer.Length);
infile.Close();
FileStream outfile = File.Create(savefile);

GZipStream gzipStream = new GZipStream(outfile, CompressionMode.Compress, false);
gzipStream.Write(buffer, 0, buffer.Length);
gzipStream.Close();
gzipStream.Dispose();

When decompressing you need to change "DeflateStream" to "GZipStream". So instead of
C#
DeflateStream deflateStream = new DeflateStream(infile, CompressionMode.Decompress);

just use:
C#
GZipStream deflateStream = new GZipStream(infile, CompressionMode.Decompress);
 
Share this answer
 
Comments
Mostafa Elsadany 27-Jan-11 13:48pm    
thanks for help very good

but i want save oraginal extension to when i decompress
can you give me code to decompress and compress with save extension of file with zip file
thanks for your help
Espen Harlinn 29-Jan-11 4:48am    
5+ Good work :)
You should see this link
Click
Please, read the "compressing multiple files with GZipStream" topic from this link.
 
Share this answer
 
Comments
Mostafa Elsadany 27-Jan-11 14:35pm    
thanks for help

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