|
This is exactly what I was looking for! But how do I call the Zip() sub? Apparently it isn't the same as the unZIP sub.
modified 19-Nov-12 11:53am.
|
|
|
|
|
the zipping sub should be :
Sub Zip(ByVal inputFolder As String, ByVal outputZip As String)
Dim startBuffer() As Byte = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, _
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
FileIO.FileSystem.WriteAllBytes(outputZip, startBuffer, False)
Dim sc As New Shell32.Shell()
Dim input As Shell32.Folder = sc.NameSpace(inputFolder)
Dim output As Shell32.Folder = sc.NameSpace(outputZip)
output.CopyHere(input.Items, 16)
End Sub
Calling it is just as simple as calling the unzip() sub :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Zip("D:\myfiles", "D:\myZip.zip")
End Sub
|
|
|
|
|
Since this runs in a separate process, but is still under the same process name, I can't figure out how to make my program wait until it is finished compressing.
I thought maybe I could have it loop until the compressed file is made. But with this code, it makes the file, then fills it. So I am completely lost..
|
|
|
|
|
Hello ,
Don't worry it's simple.
To check if the file is being used by another program you could open it into a stream and check.
If it will throw an error this means it's being used by another process ( in our program shell32 ) otherwise shell32 has finished compressing and the file is ready to use.
So use this code to check if file being used by another program
Function isFileready(path As String) As Boolean
Try
Dim j As IO.Stream = IO.File.Open(path, IO.FileMode.Open)
j.Close()
Return (True)
Catch ex As Exception
Return (False)
End Try
End Function
So the final code for compressing a file and waiting for it until it finishes compressing :
Private Sub Compress()
Dim startBuffer() As Byte = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, _
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
FileIO.FileSystem.WriteAllBytes("d:\test.zip", startBuffer, False)
Dim sc As New Shell32.Shell()
Dim input As Shell32.Folder = sc.NameSpace("D:\appdata\HMMI\test")
Dim output As Shell32.Folder = sc.NameSpace("D:\test.zip")
output.CopyHere(input.Items, 16)
Do Until FileLen("D:\test.zip") > 22
Loop
Do Until isFileready("D:\test.zip") = True
Loop
Me.Text = "done!"
End Sub
Function isFileready(path As String) As Boolean
Try
Dim j As IO.Stream = IO.File.Open(path, IO.FileMode.Create)
j.Close()
Return (True)
Catch ex As Exception
Return (False)
End Try
End Function
Best Regards
|
|
|
|
|
You are my hero. Thank you. I will implement this today.
|
|
|
|
|
|
It is telling me that the "FileLen" cannot find my file "test.zip"
Sub unZIP(ByVal inputZip As String, ByVal outputFolder As String)
Dim sc As New Shell32.Shell()
IO.Directory.CreateDirectory(outputFolder)
Dim output As Shell32.Folder = sc.NameSpace(outputFolder)
Dim input As Shell32.Folder = sc.NameSpace(inputZip)
output.CopyHere(input.Items, 16)
End Sub
Sub Zip(ByVal inputFolder As String, ByVal outputZip As String)
Dim startBuffer() As Byte = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, _
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
FileIO.FileSystem.WriteAllBytes(outputZip, startBuffer, False)
Dim sc As New Shell32.Shell()
Dim input As Shell32.Folder = sc.NameSpace(inputFolder)
Dim output As Shell32.Folder = sc.NameSpace(outputZip)
output.CopyHere(input.Items, 16)
End Sub
...
'Compress testdirectory to test.zip
Zip(AppData & "\HMMI\testdirectory", AppData & "\HMMI\test.zip")
'Wait for compression to finish
Do Until FileLen(AppData & "\HMMI\test.zip") > 22
Loop
Do Until isFileready(AppData & "\HMMI\test.zip") = True
Loop
Anything else I make can see it perfectly, and I've physically checked to make sure it is there. Not sure why it can't see it?
|
|
|
|
|
hmmm weird
Ok try putting this line before the Filelen function to make sure the file has been created :
Do Until FileIO.FileSystem.FileExists(AppData & "\HMMI\test.zip") = True
Loop
i mean it should be like this :
Do Until FileIO.FileSystem.FileExists(AppData & "\HMMI\test.zip") = True
Loop
Do Until FileLen(AppData & "\HMMI\test.zip") > 22
Loop
Do Until isFileready(AppData & "\HMMI\test.zip") = True
Loop
|
|
|
|
|
Wow, I'm really amazed that didn't work. Still says FileLen cannot find it. I mean, the "file exists" loop proves that it is actually there or it wouldn't even get to the FileLen command.
modified 19-Nov-12 16:42pm.
|
|
|
|
|
aww
maybe your application doesn't have permissions to access that file ? i mean the directory that you are doing your operation in it might require administrator permissions.
So to make sure everthing is OK , try the program with another file that's in a directory that doesn't need permission , let's say like the D:\
try it
|
|
|
|
|
I've tried that, and a ton of other ways. But I think I've got an idea. Is there a way that I can make Sub Zip create a file/directory without interfering with the Sub itself?
Something like -
Sub Zip(ByVal inputFolder As String, ByVal outputZip As String)
Dim startBuffer() As Byte = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, _
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
FileIO.FileSystem.WriteAllBytes(outputZip, startBuffer, False)
Dim sc As New Shell32.Shell()
Dim input As Shell32.Folder = sc.NameSpace(inputFolder)
Dim output As Shell32.Folder = sc.NameSpace(outputZip)
output.CopyHere(input.Items, 16)
System.IO.Directory.CreateDirectory(Appdata & "\testdirectory")
End Sub
This would have worked but the "CreateDirectory" line messed with the Sub Zip.
|
|
|
|
|
ehm .. put this in the beginning of the sub
If IO.Directory.Exists(Appdata & "\testdirectory") = False Then
System.IO.Directory.CreateDirectory(Appdata & "\testdirectory")
End If
|
|
|
|
|
|
oh no i never get desperate.
The thing is that when u first asked about it, i did exactly what you have asked for and it worked on my pc =/ .... i don't know why it's not working on yours ..
hey how about sending me your project ?
I'm a good debugger i promise nothing of your project files will be used here or there so you are safe.
If you feel OK about that then send it to me : bashar.tahir@yahoo.com
I will debug it from the beginning, test everything and fix the errors.
Best Regards
|
|
|
|
|
I trust you. But obviously that will be my last resort (don't be surprised if it ends up happening).
Good News:
I re-implemented the solution you originally gave me. This time, I didn't get a "couldn't find file" error.
Bad News:
It pretty much skipped over it. Basically it started the compression and then continued doing the rest of the code in the backgroundworker.
As if -
Do Until FileLen(AppData & "\NBMI\ModdedJar.zip") > 22
Loop
Do Until isFileready(AppData & "\NBMI\ModdedJar.zip") = True
Loop
Wasn't even there.
|
|
|
|
|
ah great.
just increase length from 22 bytes to maybe 50 ?
check it but don't go too high.
pick up a length that's < 200 assuming that your files-to-be-zipped size are larger than 178 bytes
so just change 22 to 50 or 30
Try it, if it didn't work then I have another completely different method for checking if the file is being used by another process , it's a little slower but it works
So change the length first if it didn't work then we will start with the 2nd method
|
|
|
|
|
Try it, if it didn't work then I have another completely different method for checking if the file is being used by another process , it's a little slower but it works
So change the length first if it didn't work then we will start with the 2nd method
|
|
|
|
|
Didn't seem to change. What do you mean by length?
What is your second method?
|
|
|
|
|
i mean like this :
change the length from >22 to >100
Do Until FileLen(AppData & "\NBMI\ModdedJar.zip") > 100
Loop
Do Until isFileready(AppData & "\NBMI\ModdedJar.zip") = True
Loop
Also you may add this in between (although adding this is a bit cranky ):
System.Threading.Thread.Sleep(100)
like this :
Do Until FileLen(AppData & "\NBMI\ModdedJar.zip") > 100
Loop
System.Threading.Thread.Sleep(100)
Do Until isFileready(AppData & "\NBMI\ModdedJar.zip") = True
Loop
i guess it is now a "must work" code
|
|
|
|
|
Alright, don't hate me, but it is still just skipping over it. This is a pretty large zip file. Maybe I should try something crazy like 300?
|
|
|
|
|
GOT IT WORKING! Will post final code in just a second~
Edit----
You have to make sure to set Administrator privileges in the form. This makes the compression process dominant and won't break when the zip is read. You also have to sleep the thread before you check it the first time because it checks it before it can be created.
Zip and Counter are Dim(ed) earlier
'Compress test to test.zip
Zip("C:\test", "C:\test.zip")
Threading.Thread.Sleep(3000)
'Wait for compression to finish
counter = 1
While (counter <= 2)
Threading.Thread.Sleep(1000)
Try
My.Computer.FileSystem.ReadAllBytes("C:\test.zip")
Catch
counter = 1
End Try
counter += 1
End While
rest of sub or BGW ~
modified 21-Nov-12 17:26pm.
|
|
|
|
|
Well ****. It worked perfectly for the first compression but whenever I try to use the same process on another compression I get .zip not found errors or .zip is being used by another program errors. I think it is because this folder that is being zipped is larger than the last. I think I can make it work if I increase the thread sleep time. But then this will mess up on slower computers because they will take longer to compress files.
|
|
|
|
|
a last try , please start a new project and add the shell32 object then add this code:
Please don't change anything
Private Sub Zip(ByVal inputFolder As String, ByVal outputZip As String)
Dim startBuffer() As Byte = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, _
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
FileIO.FileSystem.WriteAllBytes(outputZip, startBuffer, False)
Dim sc As New Shell32.Shell()
Dim input As Shell32.Folder = sc.NameSpace(inputFolder)
Dim output As Shell32.Folder = sc.NameSpace(outputZip)
output.CopyHere(input.Items, 16)
Do Until My.Computer.FileSystem.FileExists(outputZip) = True
Loop
Me.Text = "compressing!"
Refresh()
System.Threading.Thread.Sleep(100)
Do Until isFileready(outputZip) = True
Loop
Me.Text = "done!"
End Sub
Function isFileready(path As String) As Boolean
Try
Dim j As IO.Stream = IO.File.Open(path, IO.FileMode.Open)
j.Close()
Return (True)
Catch ex As Exception
Return (False)
End Try
End Function
Call the sub with an input folder and an output zip and tell me if it works or not
modified 23-Nov-12 3:04am.
|
|
|
|
|
I will set up an entirely new project for this.
Can I call this sub from a backgroundworker like I have in the past?
Should I run the program/project with administrator privileges?
|
|
|
|
|
No please just copy the code and run it this way.
If it works then make any edit you want
Best Regards
|
|
|
|
|