压缩文件问题 [英] problem with compress files

查看:65
本文介绍了压缩文件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

压缩文件问题
此代码
压缩,但这里出现了第一个问题:我无法使用文件扩展名保存
我更改为zip,如果使用doc或其他
,则无法使用原始扩展名进行压缩

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

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

int bytesRead = deflateStream.Read(buffer, offset, 100);




to

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


我仍然有此错误
请任何帮助
并感谢您的帮助

[修改:添加了前置标签...它们使世界变得更漂亮了!还修复了制表符并摆脱了textspeak的问题.]


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.]

推荐答案



我已经测试了您的代码,并更改了以下内容以使其对我有用:
压缩时,可以将SaveFileDialog的Filter设置为"zip",而不是事后更改扩展名:
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:
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();


解压缩时,需要将"DeflateStream"更改为"GZipStream".因此,而不是


When decompressing you need to change "DeflateStream" to "GZipStream". So instead of

DeflateStream deflateStream = new DeflateStream(infile, CompressionMode.Decompress);


只需使用:


just use:

GZipStream deflateStream = new GZipStream(infile, CompressionMode.Decompress);


您应该会看到此链接
点击
请从此链接阅读使用GZipStream压缩多个文件"主题.
You should see this link
Click
Please, read the "compressing multiple files with GZipStream" topic from this link.


这篇关于压缩文件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆