Can't delete input file after extraction.

Nov 16, 2010 at 11:41 AM

After I extract a multi volume zip archive with method ZipEntry.Extract(baseDirectory), I can't delete the input file because it is locked by the DotNetZip library.

I get exceptions like:

The process cannot access the file 'c:\test\EDRM-multivolume.z01' because it is being used by another process.

However the code is single threaded. Errors like this happen when a file is read via a file stream and this file stream isn't properly disposed. I don't think that it is related to other processes or other threads.

How can I solve this problem?

 

 

Coordinator
Nov 16, 2010 at 2:02 PM

Show your code.

Did you use a using clause?  That is what disposes the ZipFile object, and closes files related to it.

 

Nov 17, 2010 at 1:11 PM

Here is an example, the first using(FileStream f = new FileStream) runs fine but the second time it returns an exception that the file can't be accessed, the extraction itself was successful:

            using (FileStream f = new FileStream(@"c:\test\EDRM-multivolume.z01", FileMode.Open, FileAccess.Write)) {
            }
            
            int counter = 0; 
            using (ZipFile file = new ZipFile(@"c:\test\EDRM-multivolume.zip")) {
                List entries = file.Entries.ToList();
                foreach (ZipEntry entry in entries) {
                    counter++;
                    if (!entry.IsDirectory) {
                        entry.FileName = @"result" + counter.ToString() + ".txt";
                        entry.Extract(@"C:\testResults"); 
                    }
                }
            }

            using (FileStream f = new FileStream(@"c:\test\EDRM-multivolume.z01", FileMode.Open, FileAccess.Write)) {
            }

My aim is to delete the archive after it has been extracted.

 

Coordinator
Nov 17, 2010 at 3:30 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Nov 17, 2010 at 3:44 PM

RaymondRaymond, what you report sounds like a bug to me.

I'll have to investigate. I don't have any good suggestions for you, right now, on how to avoid the problem.

 

Nov 17, 2010 at 7:10 PM

Is it possible to extract a multi volume zip by using streams of a big concatenated file? 

Coordinator
Nov 17, 2010 at 11:57 PM

Raymond, I don't know what you mean.

Maybe you could elaborate, or show some code illustrating what you are talking about.

 

Nov 19, 2010 at 8:18 AM

Well, it is possible to extract streams. Is it possible to create a big stream that contains all the contents of all the files of the archive and to extract that stream?

Or is it possible to create one big file that contains all the contents of all the files of the archive and to extract that big file?

I understand that the order of concatanation will be important.

 

Coordinator
Nov 19, 2010 at 11:45 AM

Sure.  There's a method, ZipEntry.Extract(stream).  I believe you can simply call that method, for each entry in the archive, and pass the same stream. This method does not close the destination stream after extracting contents into it.  The flow is like this:

Open the destination stream
For each entry

extract the entry into that stream

 

 

Nov 19, 2010 at 4:07 PM

I use C# 4.0.

I tried this:

            using (MemoryStream memoryStream = new MemoryStream()) {
                using (FileStream t = new FileStream(@"c:\test\EDRM-multivolume.zip", FileMode.Open)) {
                    t.CopyTo(memoryStream);
                }
                using (FileStream t = new FileStream(@"c:\test\EDRM-multivolume.z01", FileMode.Open)) {
                    t.CopyTo(memoryStream);
                }
                using (FileStream t = new FileStream(@"c:\test\EDRM-multivolume.z02", FileMode.Open)) {
                    t.CopyTo(memoryStream);
                }
                using (FileStream t = new FileStream(@"c:\test\EDRM-multivolume.z03", FileMode.Open)) {
                    t.CopyTo(memoryStream);
                }
                using (FileStream t = new FileStream(@"c:\test\EDRM-multivolume.z04", FileMode.Open)) {
                    t.CopyTo(memoryStream);
                }
                using (FileStream t = new FileStream(@"c:\test\EDRM-multivolume.z05", FileMode.Open)) {
                    t.CopyTo(memoryStream);
                }
                using (FileStream t = new FileStream(@"c:\test\EDRM-multivolume.z06", FileMode.Open)) {
                    t.CopyTo(memoryStream);
                }
                using (FileStream t = new FileStream(@"c:\test\EDRM-multivolume.z07", FileMode.Open)) {
                    t.CopyTo(memoryStream);
                }
                using (FileStream t = new FileStream(@"c:\test\EDRM-multivolume.z08", FileMode.Open)) {
                    t.CopyTo(memoryStream);
                }

                memoryStream.Position = 0; 

                using (ZipFile file = ZipFile.Read(memoryStream)) {
                }
            }

but I get exception:
Ionic.Zip.BadReadException was unhandled
  Message=  ZipEntry::ReadHeader(): Bad signature (0x4139E2FB) at position  0x00000000
  Source=Ionic.Zip
  StackTrace:
       at Ionic.Zip.ZipEntry.ReadHeader(ZipEntry ze, Encoding defaultEncoding)
       at Ionic.Zip.ZipEntry.ReadEntry(ZipContainer zc, Boolean first)
       at Ionic.Zip.ZipFile.ReadIntoInstance_Orig(ZipFile zf)
       at Ionic.Zip.ZipFile.ReadIntoInstance(ZipFile zf)
       at Ionic.Zip.ZipFile.Read(Stream zipStream, TextWriter statusMessageWriter, Encoding encoding, EventHandler`1 readProgress)
       at Ionic.Zip.ZipFile.Read(Stream zipStream, TextWriter statusMessageWriter, Encoding encoding)
       at Ionic.Zip.ZipFile.Read(Stream zipStream)
on line ZipFile.Read(memoryStream).