Getting error "Cannot access a closed file."

Jan 13, 2010 at 10:32 AM

I have a method

 private void SaveOutputFile(string targetFile, ZipFile sourceZipFile)
{
try { string strZipFolder = Path.GetDirectoryName(sourceZipFile.Name);
string strInfoXmlFile = "info.xml";
XmlDocument objXmlDoc = GetJobXml(objJobInfoXml);
sourceZipFile.AddEntry(strInfoXmlFile, objXmlDoc.InnerXml); sourceZipFile.Save(targetFile); } catch (Exception ex)
{
throw ex;
}
}

In which I am adding a File to an Entry in an existing Zipfile then Saving the Zipfile into a new location

But for one zipfile i am getting this error "Cannot access a closed file."

Even without adding a new entry I am getting same error for a particular zip file.

Please let me know what I amy be doing wrong.

Jan 13, 2010 at 10:33 AM
Edited Jan 13, 2010 at 10:43 AM

Message = Cannot access a closed file

Source = mscorlib

Full Stack Trace is

 

   at System.IO.__Error.FileNotOpen()
   at System.IO.FileStream.Seek(Int64 offset, SeekOrigin origin)
   at Ionic.Zip.CountingStream.Seek(Int64 offset, SeekOrigin origin)
   at Ionic.Zip.ZipEntry.CopyThroughWithNoChange(Stream outstream)
   at Ionic.Zip.ZipEntry.CopyThroughOneEntry(Stream outstream)
   at Ionic.Zip.ZipEntry.Write(Stream s)
   at Ionic.Zip.ZipFile.Save()
   at Ionic.Zip.ZipFile.Save(String fileName)
   at BO.Level1Validator.SaveOutputFile(String targetFile, ZipFile sourceZipFile) in C:\MyProject\LevelValidator.cs:line 799

 

It is looks like a error close to

http://community.sharpdevelop.net/forums/p/6881/19716.aspx

 

Coordinator
Jan 13, 2010 at 6:39 PM
Edited Jan 13, 2010 at 6:42 PM

The error you're getting has nothing to do with an error in SharpZipLib. 

They are different implementations.  Any similarities are coincidental.

The stack trace indicates that you are trying to update a zipfile.  At least one of the entries in the existing zip file is unchanged. DotNetZip will copy through the metadata+data for each unchanged zip entry, from the source to the target zip file.

To do this, DotNetZip needs to seek within the original (source) Zip file, to the location of the original entry.  It then does a read/write copy loop.  But the Seek is failing because the original, source zip file is closed.

I don't kniow why that would be the case.  I'd like to see more of your code - for example, how do you instantiate the ZipFile object, before passing it to the method you included above?