无法提取.zip文件.获取InvalidDataException [英] Can't extract .zip file. Getting InvalidDataException

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

问题描述

我有一个很大的zip文件,其中包含其他zip文件.我希望我的c#程序能够识别出所选文件是一个zip文件,如果它是一个zip文件,则将其提取到与zip文件位于相同位置的文件夹中.我的代码是 在这里:

I have a large zip file which contains other zip files inside of it. I want my c# program to be able to recognize that the selected file is a zip file and if it is a zip file, then to extract it to a folder in the same location as the zip file. My code is here:

		private void Unzip(OpenFileDialog tvZipOpen)
		{
			string zipFile = tvZipOpen.FileName; // file to unzip
			int i = zipFile.LastIndexOf(".zip");
			string targetDirectory = zipFile.Substring(0, i); // location to extract to
			ZipFile.ExtractToDirectory(zipFile, targetDirectory);
			tvZipOpen.InitialDirectory = targetDirectory;
			tvZipOpen.ShowDialog();
		}

我正在使用.NET 4.5中的ZipFile类,并且在这里调用此方法:

I am using the ZipFile class from .NET 4.5 and I call on this method here:

			if (tvOpen.ShowDialog() == DialogResult.OK)
			{
				while (tvOpen.FileName.ToLower().EndsWith(".zip"))
				{
					Unzip(tvOpen);
				}
				return tvOpen.FileNames;
			}


该代码可以很好地提取第一个zip文件,但是当我尝试提取第二个zip文件时,我收到一个InvalidDataException异常,该异常表示本地文件头已损坏.但是,我认为它没有损坏,因为我能够打开并解压缩zip文件. 完美地在Windows资源管理器(也使用.NET)中.为什么在Windows资源管理器中打开/解压缩zip文件没有问题,但是当我尝试在代码中进行操作时,我得到了InvalidDataException?该问题的解决方案是什么?任何 帮助将不胜感激.


The code works fine for extracting the first zip file but when I try to extract the second zip file, I get an InvalidDataException that says local file header is corrupt. However, I don't think it is corrupt because I am able to open and extract the zip files perfectly in the windows explorer (which also uses .NET). How come I don't have a problem opening/extracting the zip files in windows explorer but when I try to do it in my code I get that InvalidDataException? and what is the solution to this problem? Any help would be greatly appreciated.

推荐答案

我已经运行了您的代码,并且没有得到您所描述的损坏的标头...我确实得到了选择打开"后出现新对话框,并且如果我不更改所选文件,我会得到一个文件已存在错误,因为该文件已提取到新目录中.也许是在 提取后的清理方式或清理方式.以下是我的代码.

I've ran your code and don't get a corrupt header as you describe... I do get a new dialog after selecting open and if I don't change the selected file I get a file already exists error because the file is extracted in a new dir.  Perhaps it is in the way you are calling it or cleaning up after the extract.  The following is the code I have.

private void button1_Click(object sender, EventArgs e){    OpenFileDialog tvOpen = new OpenFileDialog();    tvOpen.Title = "Open";    //op.Filter = "AWM Files(*.tbt,*.cmp)|(*.tbt;*.cmp)|All Files(*.*)|*.*|Text Document(*.txt)|*.txt";    if (tvOpen.ShowDialog() == DialogResult.OK)    {         while (tvOpen.FileName.ToLower().EndsWith(".zip"))         {            Unzip(tvOpen);         }    } }private void Unzip(OpenFileDialog tvOpen){    string zipFile = tvOpen.FileName; // file to unzip    int i = zipFile.LastIndexOf(".zip");    string targetDirectory = zipFile.Substring(0, i); // location to extract to    ZipFile.ExtractToDirectory(zipFile, targetDirectory);    tvOpen.InitialDirectory = targetDirectory;    tvOpen.ShowDialog();}



这篇关于无法提取.zip文件.获取InvalidDataException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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