You sure are having a bit of an issue!!!!!! :)
Before you even attempt to go any further, I stringly recommend that you
'turn' Option Strict and Option Explicit 'on' for ALL your VB.NET projects
and solutions. You can do this in Tools/Options. Expand the Projects and
Solutions node and select VB Defaults. Settings those to 'on' in there will
ensure that Option Strict and Option Explicit are automatically 'on' for all
new projects. For existing projects you will need to change the settings for
each individual project.
Option Explicit 'on' means that you must explicitly define each variable
before you can use it.
Option Strict 'on' means that the complier will make NO assumptions about
what the code should do. When you turn it on for this project than a number
of compile-time 'errors' will appear and you will need to correct them all.
A number of the problems in the code fragment are caused by Option Strict
being 'off'. If you leave it turned off then you will continue to tear your
hair out because you not be able to spot various subtle problems.
Now for the nitty gritty.
Dim arystrmFolderNa mes() As String =
Directory.GetDi rectories(strSo urceDir)
This line create a string array that contains 1 element for each sub-folder
in whatever folder strSourceDir is pointing to. Each element contains the
name of (or the path to) a single sub-folder.
With the line (inside the loop):
Dim strmFile As FileStream = File.OpenRead(s trFolder)
you are attempting to open a sub-folder for reading and that is where you
are falling over. A folder (or directory) cannot be opened as if it were a
file. You need to walk the 'tree' and deal with each and every file in each
and every sub-folder.
Before we deal with that, the line:
Dim strTargetFile As String = strTargetDir &
arystrmFolderNa mes(intCounter) .LastIndexOfAny ("\")
must also be causing you problems. If your strTargetDir is "C:\Temp\" and
arystrmFolderNa mes(intCounter) is "C:\Data\Su bA" then strTargetFile will
become "C:\Temp\7" and I don't really think that is what you intend. I
suspect that you are looking for strTargetFile to be "C:\Temp\SubA.z ip". If
that is what you are looking for than you need to use something like:
Dim strTargetFile As String =
Path.ChangeExte nsion(Path.Comb ine(strTargetDi r,
Path.GetFileNam e(arystrmFolder Names(intCounte r))), "zip")
You use the phrase 'as the program trys to recurse through the subfolders in
FromFolder', but what your code is, in fact, doing is iterating through the
subfolders in FromFolder.
You do actually need to recursively walk the tree that starts at FromFolder,
dealing with every sub-folder including nested sub-folders and handling
every file that you encounter. This can be done with something like:
Private Sub RecurseFolders( ByVal folder as String)
Dim _files as String() = Directory.GetFi les(folder)
For Each _file As String In _files
Dim strmFile As FileStream = File.OpenRead(_ file)
Dim abyBuffer(strmF ile.Length - 1) As Byte
strmFile.Read(a byBuffer, 0, abyBuffer.Lengt h)
strmFile.Close( )
Dim objZipEntry As New ZipEntry(_file)
objZipEntry.Dat eTime = DateTime.Now
objZipEntry.Siz e = abyBuffer.Lengt h
strmZipOutputSt ream.PutNextEnt ry(objZipEntry)
strmZipOutputSt ream.Write(abyB uffer, 0, abyBuffer.Lengt h)
Next
Dim _folders As String() = Directory.GetDi rectories(folde r)
For Each _folder in _folders
RecurseFolders( FromFolder)
Next
End Sub
Call this method for the appropriate place with:
RecurseFolders( FromFolder)
Let us know how you get on.
"Bruce W. Darby" <kr****@comcast .netwrote in message
news:FK******** *************** *******@comcast .com...
>
"Rad [Visual C# MVP]" <no****@nospam. comwrote in message
news:rn******** *******@thinker sroom.com...
>If file size is paramount you might want to consider using better
compression formats (rar, bzip, etc). You can use sharpziplib to
accomplish
this sort of thing.
http://www.icsharpcode.net/OpenSourc...b/Default.aspx
--
Bits.Bytes
http://bytes.thinkersroom.com
Took a look into the sharpziplib you suggested and found it easier than
usual to add it to my solution, but I am having a bit of an issue. Not
sure what I'm doing wrong and hope that you can enlightem my fuzzy brain.
When I start the compression, I get an UnauthorizedAcc essException thrown
as the program trys to recurse through the subfolders in FromFolder. I can
understand that it's treating the subdirectories as files, but cannot
figure out why. Any tweaks or nudges? Code follows...
Private Sub CompressFolder( )
Dim strSourceDir As String = txtSelectDir.Te xt
If strSourceDir.Le ngth = 0 Then
MsgBox("Please specify a directory")
txtSelectDir.Fo cus()
Exit Sub
Else
If Not Directory.Exist s(strSourceDir) Then
MsgBox("Directo ry not found")
txtSelectDir.Fo cus()
Exit Sub
End If
End If
Dim strTargetDir As String = txtWriteDir.Tex t
If strTargetDir.Le ngth = 0 Then
MsgBox("Please specify a directory")
txtWriteDir.Foc us()
Exit Sub
Else
If Not Directory.Exist s(strTargetDir) Then
MsgBox("Directo ry not found")
txtWriteDir.Foc us()
Exit Sub
End If
End If
Dim arystrmFolderNa mes() As String =
Directory.GetDi rectories(strSo urceDir)
Dim strmZipOutputSt ream As ZipOutputStream
Dim intCounter As Integer
Dim strTargetFile As String = strTargetDir &
arystrmFolderNa mes(intCounter) .LastIndexOfAny ("\")
strmZipOutputSt ream = New ZipOutputStream (File.Create(st rTargetFile))
strmZipOutputSt ream.SetLevel(9 )
Dim strFolder As String
For Each strFolder In arystrmFolderNa mes
Dim strmFile As FileStream = File.OpenRead(s trFolder)
Dim abyBuffer(strmF ile.Length - 1) As Byte
strmFile.Read(a byBuffer, 0, abyBuffer.Lengt h)
Dim objZipEntry As ZipEntry = New ZipEntry(strFol der)
objZipEntry.Dat eTime = DateTime.Now
objZipEntry.Siz e = strmFile.Length
strmFile.Close( )
strmZipOutputSt ream.PutNextEnt ry(objZipEntry)
strmZipOutputSt ream.Write(abyB uffer, 0, abyBuffer.Lengt h)
intCounter += 1
Next
strmZipOutputSt ream.Finish()
strmZipOutputSt ream.Close()
MsgBox("Operati on completed")
End Sub
P.S. Please forgive my sloppy coding