解压缩文件错误
我正在使用www.icsharpcode.net上的SharpZipLib开源.net库
我的目标是解压缩xml文件并将其读入数据集。 但是,在将文件读入数据集时出现以下错误:“根级别的数据无效。第1行,位置1”。 我相信正在发生的事情是解压缩代码没有释放文件,原因如下。
1.)如果我解压缩文件并退出应用程序。 当我重新启动应用程序时,我可以将解压缩的文件读入数据集。 2.)如果我在写完之后立即读入xml文件(没有压缩),那么它工作正常。
3.)如果我将数据集写入xml,将其压缩,解压缩,然后尝试读取它,我得到exception。
下面的代码很简单。 UnZipFile将返回刚解压缩的文件名。 在此调用下方是调用将其读入数据集。 变量fileToRead是新解压缩的xml文件的完整路径。
string fileToRead = UnZipFile(filepath, DOViewerUploadStoreArea); ds.ReadXml(fileToRead ) private string UnZipFile(string file, string dirToUnzipTo) { string unzippedfile = ""; try { ZipInputStream s = new ZipInputStream(File.OpenRead(file)); ZipEntry myEntry; string tmpEntry = String.Empty; while ((myEntry = s.GetNextEntry()) != null) { string directoryName = dirToUnzipTo; string fileName = Path.GetFileName(myEntry.Name); string fileWDir = directoryName + fileName; unzippedfile = fileWDir; FileStream streamWriter = File.Create(fileWDir); int size = 4096; byte[] data = new byte[4096]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } streamWriter.Close(); } s.Close(); } catch (Exception ex) { LogStatus.WriteErrorLog(ex, "ERROR", "DOViewer.UnZipFile"); } return (unzippedfile); }
那么,最终文件是什么样的? (与原始相比)。 您不会显示压缩代码,这可能是拼图的一部分,尤其是当您部分吞下exception时。
我也会尝试确保所有IDisposable
都是Dispose()
d,理想情况下通过using
; 另外 – 如果问题出在路径构造上,请使用Path.Combine
。 请注意,如果myEntry.Name
包含子目录,则需要手动创建它们。
这就是我所拥有的 – 它适用于解压缩ICSharpCode.SharpZipLib.dll:
private string UnZipFile(string file, string dirToUnzipTo) { string unzippedfile = ""; try { using(Stream inStream = File.OpenRead(file)) using (ZipInputStream s = new ZipInputStream(inStream)) { ZipEntry myEntry; byte[] data = new byte[4096]; while ((myEntry = s.GetNextEntry()) != null) { string fileWDir = Path.Combine(dirToUnzipTo, myEntry.Name); string dir = Path.GetDirectoryName(fileWDir); // note only supports a single level of sub-directories... if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); unzippedfile = fileWDir; // note; returns last file if multiple using (FileStream outStream = File.Create(fileWDir)) { int size; while ((size = s.Read(data, 0, data.Length)) > 0) { outStream.Write(data, 0, size); } outStream.Close(); } } s.Close(); } } catch (Exception ex) { Console.WriteLine(ex); } return (unzippedfile); }
问题还可能在于编写zip的代码或读取生成的文件的代码。
我使用TextPad将原始版本与最终版本进行了比较,它们完全相同。 我还重写了代码以利用使用。 这是代码。 我的问题似乎集中在文件锁定等方面。 如果我解压缩文件退出应用程序然后启动它将读取查找。
private string UnZipFile(string file, string dirToUnzipTo) { string unzippedfile = ""; try { using (ZipInputStream s = new ZipInputStream(File.OpenRead(file))) { ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = dirToUnzipTo; string fileName = Path.GetFileName(theEntry.Name); string fileWDir = directoryName + fileName; unzippedfile = fileWDir; if (fileName != String.Empty) { using (FileStream streamWriter = File.Create(fileWDir)) { int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } } } } } } catch (Exception ex) { LogStatus.WriteErrorLog(ex, "ERROR", "DOViewer.UnZipFile"); } return (unzippedfile); }
这与DotNetZip相比要简单得多。
using (ZipFile zip = ZipFile.Read(ExistingZipFile)) { zip.ExtractAll(TargetDirectory); }
如果你想决定要提取哪些文件….
using (ZipFile zip = ZipFile.Read(ExistingZipFile)) { foreach (ZipEntry e in zip) { if (wantThisFile(e.FileName)) e.Extract(TargetDirectory); } }
如果要在提取期间覆盖现有文件:
using (ZipFile zip = ZipFile.Read(ExistingZipFile)) { zip.ExtractAll(TargetDirectory, ExtractExistingFileAction.OverwriteSilently); }
或者,提取受密码保护的条目:
using (ZipFile zip = ZipFile.Read(ExistingZipFile)) { zip.Password = "Shhhh, Very Secret!"; zip.ExtractAll(TargetDirectory, ExtractExistingFileAction.OverwriteSilently); }