1

I'm looping through a list of zip files and downloading them from a WebClient. I'm trying to extract them immediately after they download, but getting an error that the file is being used by another process.

Error: The process cannot access the file 'c:\temp\test.zip' because it is being used by another process.

The only thing that I can think of is that maybe the file is still "in use" for a short time after being downloaded? DownloadFile does block ("This method blocks while downloading the resource." - http://msdn.microsoft.com/en-us/library/ez801hhe(v=vs.110).aspx), so I can't see it being an issue with the file still downloading.

Any ideas on a workaround?

Using client As New WebClient
    oLog.Info("Downloading " & strFileServer & " to " & strFileLocal & "...")
    If Not Directory.Exists(strPathLocal) Then Directory.CreateDirectory(strPathLocal)
    client.DownloadFile(strFileServer, strFileLocal)
End Using

Using archive As ZipArchive = ZipFile.OpenRead(strFileLocal)
    For Each entry As ZipArchiveEntry In archive.Entries
        oLog.Info("Extracting " & entry.FullName & " to " & strPathLocal & "...")
        entry.ExtractToFile(strFileLocal, True)
    Next
End Using
Dave Zych
  • 21,581
  • 7
  • 51
  • 66
Capellan
  • 717
  • 6
  • 15
  • You can try to wait a bit until the files are unlocked using this [implementation](http://stackoverflow.com/a/50800/1042934). – Ognyan Dimitrov Nov 17 '14 at 20:39
  • Hmm. Well it returns immediately after the first attempt, but then gives the same error again. – Capellan Nov 17 '14 at 21:02
  • Is your Anti Virus program locking the file for a small period of time. Try turning it off – Grantly Nov 17 '14 at 21:18
  • You can read [here](http://stackoverflow.com/questions/317071/how-do-i-find-out-which-process-is-locking-a-file-using-net) how to discover which process is locking your file. – Ognyan Dimitrov Nov 18 '14 at 08:10
  • I found the issue. It's a bug on my part. I'm trying to ExtractToFile using the zip file's name instead of "entry.ExtractToFile(strPathLocal & entry.FullName, True)". That explains why waiting returned immediately and handle returned nothing. :-/ Thanks for the suggestions anyway. – Capellan Nov 18 '14 at 14:29

0 Answers0