Umut Şeker
asked on
Unable to delete zipped files
I'm using SharpZipLib (http://www.icsharpcode.net/OpenSource/SharpZipLib/) library to zip a file(System.IO.Compression is not suit able for files bigger that 4 GB and doesn't support x64).
After zipping the file I couldn't delete it from windows explorer. I'm new to VB so I could miss something.
I'm sending;
Dim A As String = "C:\bck\backup.xxx"
Dim B As String = "C:\zip\backup.zip"
SimpleZip(A, B)
My code is below
Thanks,
After zipping the file I couldn't delete it from windows explorer. I'm new to VB so I could miss something.
I'm sending;
Dim A As String = "C:\bck\backup.xxx"
Dim B As String = "C:\zip\backup.zip"
SimpleZip(A, B)
My code is below
Thanks,
Private Sub SimpleZip(ByVal strFileToZip As String, ByVal strZippedFile As String)
Break up the new file location to extract the filename without the path
Dim zipparts() As String = strFileToZip.Split("\"c)
Dim zipfile As String = zipparts(zipparts.Length - 1)
Dim strmZipOutputStream As ZipOutputStream
strmZipOutputStream = New ZipOutputStream(File.Create(strZippedFile))
'strmZipOutputStream.UseZip64 = UseZip64.On
REM Compression Level: 0-9
REM 0: no(Compression)
REM 9: maximum compression
strmZipOutputStream.SetLevel(5)
Dim objZipEntry As ZipEntry = New ZipEntry(Path.GetFileName(strFileToZip))
objZipEntry.DateTime = DateTime.Now
'objZipEntry.Size = strmFile.Length
strmZipOutputStream.PutNextEntry(objZipEntry)
Dim strmFile As FileStream = File.OpenRead(strFileToZip)
Dim abyBuffer(4096) As Byte
Dim sourcebytes As Integer
sourcebytes = strmFile.Read(abyBuffer, 0, abyBuffer.Length)
While sourcebytes > 0
strmZipOutputStream.Write(abyBuffer, 0, sourcebytes)
sourcebytes = strmFile.Read(abyBuffer, 0, abyBuffer.Length)
End While
objZipEntry = Nothing
strmZipOutputStream.CloseEntry()
strmZipOutputStream.Finish()
strmZipOutputStream.Close()
strmZipOutputStream = Nothing
strmFile.Close()
strmFile = Nothing
End Sub
I don't have your zip library so I wasn't able to test that. Therefore, there are likely to still be some issues with that code, maybe even a typo or two. But, it should be enough to show you how to fix your problem. If you need an explanation for any of the other code changes, just ask.
ASKER
Hi jcoehoorn,
I'm tring your code. In 5 minutes I will return a reply.
In mean while you could download just the dll from;
http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
Thanks,
I'm tring your code. In 5 minutes I will return a reply.
In mean while you could download just the dll from;
http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
Thanks,
ASKER
No jcoehoorn
I couldn't also delete the file your code generated.
The error is;
"Cannot delete backup.zip : It is being used by another person or program. Close any programs that might be using the file and try again."
But when I looked by third party software(WhoLockMe). I saw that "explorer" locks the file.
Please help me about that because now I'm working overtime for 2 hours and this problem seem not to be solved by me :))
Thanks,
I couldn't also delete the file your code generated.
The error is;
"Cannot delete backup.zip : It is being used by another person or program. Close any programs that might be using the file and try again."
But when I looked by third party software(WhoLockMe). I saw that "explorer" locks the file.
Please help me about that because now I'm working overtime for 2 hours and this problem seem not to be solved by me :))
Thanks,
Okay, I misunderstood. I thought you wanted to delete the original, not the created file. See if this does any better:
Private Sub SimpleZip(ByVal FileToZip As String, ByVal ZippedFile As String)
If Not IO.File.Exists(FileToZip) Then
'You may want to thrown an exception here'
Exit Sub
End If
Dim ZipFileName As String = New IO.FileInfo(FileToZip).Name
'Keep the file stream handle, specifically allow delete when creating'
Dim fs As New IO.FileStream(ZippedFile, IO.FileMode.CreateNew, IO.FileAccess.Write, IO.FileShare.Delete)
Dim ZipOutputStream As New ZipOutputStream(fs)
'ZipOutputStream.UseZip64 = UseZip64.On '
REM Compression Level: 0-9
REM 0: no(Compression)
REM 9: maximum compression
ZipOutputStream.SetLevel(5)
Dim ZipEntry As New ZipEntry(ZipFileName)
ZipEntry.DateTime = Now
Dim ZipFile As New FileStream(FileToZip, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Delete)
'^^the IO.FileShare.Delete is what will fix your problem'
'Unfortunately, it also makes it possible for your file to be deleted mid-zip. You''ve been warned.'
'ZipEntry.Size = ZipFile.Length'
ZipOutputStream.PutNextEntry(ZipEntry)
Dim Buffer(4096) As Byte
Dim sourcebytes As Integer
sourcebytes = ZipFile.Read(Buffer, 0, Buffer.Length)
While sourcebytes > 0
ZipOutputStream.Write(Buffer, 0, sourcebytes)
sourcebytes = ZipFile.Read(Buffer, 0, Buffer.Length)
End While
ZipOutputStream.CloseEntry()
ZipOutputStream.Finish()
ZipOutputStream.Dispose()
ZipFile.Dispose()
'Specifically dispose the filestream underlying your zipstream'
fs.Dispose()
ZipEntry = Nothing
End Sub
ASKER
No. After I zipped the original file(backup.dat) to a zipped one(backup.zip), I wanted to delete the zipped one from Windows Explorer(file system) by simply right clicking on it and choosing delete file. But The error is;
"Cannot delete backup.zip : It is being used by another person or program. Close any programs that might be using the file and try again."
Thanks,
"Cannot delete backup.zip : It is being used by another person or program. Close any programs that might be using the file and try again."
Thanks,
This is with the new version as well? In that case, IO.FileStream has an UnLock() you could use, and you could also try Flush()ing it first.
ASKER
Ok I added;
fs.Unlock(0, ZippedFile.Length)
'Specifically dispose the filestream underlying your zipstream'
fs.Dispose()
ZipEntry = Nothing
and trying again.
Thanks,
fs.Unlock(0, ZippedFile.Length)
'Specifically dispose the filestream underlying your zipstream'
fs.Dispose()
ZipEntry = Nothing
and trying again.
Thanks,
ASKER
I couldn't find where I could flush or unlock the fs :( I'm getting errors.
Could you download the dll from;
http://dfn.dl.sourceforge.net/sourceforge/sharpdevelop/SharpZipLib_0854_Bin.zip
and make a small application?
Thanks,
Could you download the dll from;
http://dfn.dl.sourceforge.net/sourceforge/sharpdevelop/SharpZipLib_0854_Bin.zip
and make a small application?
Thanks,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here are the key differences between the new code and what went before:
1) Using blocks instead of a .Dispose() call. A Using block is like putting .Dispose inside a Finally block- it guarantees disposal of the object, even if an exception is thrown. It also takes the object out of scope, which may be the big difference here.
2) Added " Or FileShare.ReadWrite" to the access permissions. If you allow delete, read/write shouldn't be necessary, but since what we were doing wasn't working it was worth a shot.
This was the first thing I tried. You might try removing the FileShare.ReadWrite to see if the Using blocks alone were good enough, and if that works try removing FileShare.Delete as well, since allowing these permissions while the zip is still in progress is dangerous.
1) Using blocks instead of a .Dispose() call. A Using block is like putting .Dispose inside a Finally block- it guarantees disposal of the object, even if an exception is thrown. It also takes the object out of scope, which may be the big difference here.
2) Added " Or FileShare.ReadWrite" to the access permissions. If you allow delete, read/write shouldn't be necessary, but since what we were doing wasn't working it was worth a shot.
This was the first thing I tried. You might try removing the FileShare.ReadWrite to see if the Using blocks alone were good enough, and if that works try removing FileShare.Delete as well, since allowing these permissions while the zip is still in progress is dangerous.
ASKER
Good Morning jcoehoorn,
You are perfect but my computer not :)))
Yesterday I tried using the new function(Sub) on my application and today I tried your console application and both didn't worked(Worked but couldn't success to delete zipped file).
Ok now it's clear that there is some thing wrong with my computer. :))
You know when I looked by third party software(WhoLockMe), I saw that "C:\Windows\explorer.exe" locks the file so I'll try to focus on that.
Thanks very much for your perfect support and expertise.
You are perfect but my computer not :)))
Yesterday I tried using the new function(Sub) on my application and today I tried your console application and both didn't worked(Worked but couldn't success to delete zipped file).
Ok now it's clear that there is some thing wrong with my computer. :))
You know when I looked by third party software(WhoLockMe), I saw that "C:\Windows\explorer.exe" locks the file so I'll try to focus on that.
Thanks very much for your perfect support and expertise.
ASKER
Yeah! At last I found the problem;
I like using freeware, beta, shareware programs so I was using Zip Genius zip program. I thought that it could lock the files. I uninstall it and now everything is fine. I have installed WinRar(trial) instead :))
Thanks,
I like using freeware, beta, shareware programs so I was using Zip Genius zip program. I thought that it could lock the files. I uninstall it and now everything is fine. I have installed WinRar(trial) instead :))
Thanks,
ASKER
great! Thank you so much!
Open in new window