file downloaded using FileStream "in use by another process"

L

Loane Sharp

Hi there

I use the FileStream object to download a zip file over the internet to my
local disk. The file downloads successfully, but when I attempt to unzip it,
I'm told that the file is in use by another process. This occurs even if I
release the object using fs.Close() and fs = Nothing.

Please help (my code is given below)

Best regards
Loane

'Create a new filestream to receive data ...
Dim fs_App As FileStream = New FileStream("C:\app.zip", FileMode.Create)

'Instantiate a new byte variable to receive data ...
datBuffer = New Byte(1023) {}

'Create a webrequest/response ...
Dim reqSync_App As WebRequest = WebRequest.Create("http://123.com/app.zip")
Dim respSync_App As WebResponse = reqSync_App.GetResponse()

'Retrieve file details (ie. file length) ...
Dim strm_App As Stream = respSync_App.GetResponseStream()
len_App = respSync_App.ContentLength()

'Declare variable to track number of bytes downloaded ...
Dim cnt_App As Integer

'Iteratively download Kb by Kb ...
While total <= len_App
cnt_App = strm_App.Read(datBuffer, 0, 874)
If cnt_App <= 0 Then Exit While
fs_App.Write(datBuffer, 0, cnt_App)
total += cnt_App
End While

'Close the response/stream/filestream ...
respSync_App.Close()
strm_App.Close()
fs_App.Close()

'Release from memory ...
respSync_App = Nothing
strm_App = Nothing
fs_App = Nothing
 
K

Ken Tucker [MVP]

Hi,

Here is an example of how to download and unzip a file. It uses
the component one zip dll which was included with the vb.net resource kit.
Sorry the offer has ended for the resource kit so I cant post a link.

Dim request As WebRequest

Dim response As WebResponse

Dim reader As Stream

Dim writer As Stream

Dim data(1023) As Byte

Dim count As Integer

Dim total As Integer

Me.Show()

Me.Text = "Downloading file ....."

Application.DoEvents()

request =
WebRequest.Create("http://www.onteorasoftware.com/downloads/multigrids.zip")

response = request.GetResponse()

reader = response.GetResponseStream()

ProgressBar1.Maximum = CInt(response.ContentLength)

ProgressBar1.Value = 0

total = 0

writer = File.Create("mylocaldata.zip")

While True

count = reader.Read(data, 0, 1024)

If count <= 0 Then

Exit While

End If

writer.Write(data, 0, count)

total += 1024

If total < ProgressBar1.Maximum Then ProgressBar1.Value = total

Application.DoEvents()

End While

reader.Close()

writer.Close()

Dim mzip As New C1.C1Zip.C1ZipFile

'

mzip.Open("mylocaldata.zip")

Me.Text = "Extracting............."

Me.ProgressBar1.Value = 0

Me.ProgressBar1.Maximum = mzip.Entries.Count

Dim z As Integer = 0

For Each x As C1.C1Zip.C1ZipEntry In mzip.Entries

Dim fiZip As New System.IO.FileInfo(x.FileName)

Dim strDir As String = fiZip.DirectoryName.ToString

If Not Directory.Exists(strDir) Then Directory.CreateDirectory(strDir)

Trace.WriteLine(x.FileName)

z += 1

ProgressBar1.Value = z

mzip.Entries.Extract(x.FileName, fiZip.FullName)

Application.DoEvents()

Next x

Me.Text = "Done"



Ken

-----------------------

Hi there

I use the FileStream object to download a zip file over the internet to my
local disk. The file downloads successfully, but when I attempt to unzip it,
I'm told that the file is in use by another process. This occurs even if I
release the object using fs.Close() and fs = Nothing.

Please help (my code is given below)

Best regards
Loane

'Create a new filestream to receive data ...
Dim fs_App As FileStream = New FileStream("C:\app.zip", FileMode.Create)

'Instantiate a new byte variable to receive data ...
datBuffer = New Byte(1023) {}

'Create a webrequest/response ...
Dim reqSync_App As WebRequest = WebRequest.Create("http://123.com/app.zip")
Dim respSync_App As WebResponse = reqSync_App.GetResponse()

'Retrieve file details (ie. file length) ...
Dim strm_App As Stream = respSync_App.GetResponseStream()
len_App = respSync_App.ContentLength()

'Declare variable to track number of bytes downloaded ...
Dim cnt_App As Integer

'Iteratively download Kb by Kb ...
While total <= len_App
cnt_App = strm_App.Read(datBuffer, 0, 874)
If cnt_App <= 0 Then Exit While
fs_App.Write(datBuffer, 0, cnt_App)
total += cnt_App
End While

'Close the response/stream/filestream ...
respSync_App.Close()
strm_App.Close()
fs_App.Close()

'Release from memory ...
respSync_App = Nothing
strm_App = Nothing
fs_App = Nothing
 
L

Loane Sharp

Hi Ken
Thanks for your reply.
I tried the code, which was very similar to my own, but I get the same
problem: "(file) in use by another process". P.S. I'm using nsoftware's
IPWorksZip rather than C1Zip, but that shouldn't make a difference, should
it?
 
K

Ken Tucker [MVP]

Hi,

This works with the ipworkszip ver 6.0

Dim request As WebRequest

Dim response As WebResponse

Dim reader As Stream

Dim writer As Stream

Dim data(1023) As Byte

Dim count As Integer

Dim total As Integer

Me.Show()

Me.Text = "Downloading file ....."

Application.DoEvents()

request =
WebRequest.Create("http://www.onteorasoftware.com/downloads/multigrids.zip")

response = request.GetResponse()

reader = response.GetResponseStream()

ProgressBar1.Maximum = CInt(response.ContentLength)

ProgressBar1.Value = 0

total = 0

writer = File.Create("mylocaldata.zip")

While True

count = reader.Read(data, 0, 1024)

If count <= 0 Then

Exit While

End If

writer.Write(data, 0, count)

total += 1024

If total < ProgressBar1.Maximum Then ProgressBar1.Value = total

Application.DoEvents()

End While

reader.Close()

writer.Close()

Application.DoEvents()

Me.Text = "Extracting............."

Application.DoEvents()

zip1.ArchiveFile = "mylocaldata.zip"

zip1.ExtractToPath = "C:\Zip Test"

'Zip1.Password = "test" 'if applicable

zip1.ExtractAll()

Me.Text = "Done"



Ken

------------------------------

Hi Ken
Thanks for your reply.
I tried the code, which was very similar to my own, but I get the same
problem: "(file) in use by another process". P.S. I'm using nsoftware's
IPWorksZip rather than C1Zip, but that shouldn't make a difference, should
it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top