Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to find an XML somewhere in a zipfile or subzips

Status
Not open for further replies.

Xsi

Programmer
May 29, 2015
121
SE
Hello all

I have made a code that looks for a zip-file then take all the digits from the zipfile name and creates a folder with the name also unzip the content to the created folder.

now is my issue.
I need a loop/ a function that zip all the zipfiles inside a zip.

I have read some on google and forums but I haven't found solution yet.
Could someone help me.

this is my code so far:


Code:
Imports Microsoft.Office.Interop
Imports System
Imports System.IO
Imports System.IO.Compression

 Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim Fpath = ("C:\XML_Files_Extract")
        Dim mystring As String = "C:\X\36001-37000\36001-36100\36062\"

        Dim arFiles As String()
        arFiles = Directory.GetFiles("C:\XML_Files_Extract\", "*original från kund*.zip")
        Dim i As Integer
        For i = 0 To arFiles.Length - 1
            Dim myZipandPath As String = (arFiles(i).ToString())
            Dim MyZipDigits = CInt(Val(New Text.StringBuilder((From ch In myZipandPath.ToCharArray Where IsNumeric(ch)).ToArray).ToString))
            Dim myDigitString As String = MyZipDigits ' converted array to string
            Dim ZPath As String = (Fpath + "\" + myDigitString)
            MkDir(ZPath)
            ZipFile.ExtractToDirectory(myZipandPath, ZPath)
        Next
    End Sub

could someone help me?

Thank you in advance
 
I can't directly help at the moment, but have a look at the ZipFile class in MSDN. It seems to indicate that you need to create a directory for your Zip files and gives a very basic worked example in both C# and VB.Net.
 
Hi @Softhemc,

my opinion too I did took a look on the ZipFile class in MSDN too.
but I didn't found any solution for it anyway..
 
Hi @Softhemc,

Thank you in advance!
 
Unfortunaley over the weekend I was kidnapped by my chidren to be a chauffeur etc. So I'll get something done over the next few days I think I'm going to kidnapped several times between no and Christmas - so I'm not going to promise before Christmas.
 
haahah aww kids :)

That is fine :) I am very glad that you are helping me :)

Thank you in advance mate.
 
OK a relatively simple (but by no means comprehsive solution).

I've just used a simple Form with four buttons - which you will see in the code and a FolderBrowserDialog and an OpenFileDialog.

I have included virtually no documentation (time constrainsts), but have tried to use descriptive variable names - so it should make sense.

Most importantly I HAVE NOT INCLUDED ANY ERROR CHECKING. Any code that uses filehandling of any sort should have error checking in place.

Code:
Imports System.IO
Imports System.IO.Compression

Public Class Form1

	'To create the various folders
	Private ArchiveFolderBase As String = ""
	Private SourcesPath As String = "Sources"
	Private ExtractionsPath As String = "Extractions"
	Private ZipArchivePath As String = "ZipArchive"

	'These will have the full pathnames of all the relevant folders - this ensures that 'Relative' paths are not assumed/used
	Private SourcesFolder As String = ""
	Private ExtractionsFolder As String = ""
	Private ZipArchiveFolder As String = ""

	Private Sub SelectFolder_Click(sender As Object, e As EventArgs) Handles SelectFolderButton.Click


		If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
			ArchiveFolderBase = FolderBrowserDialog1.SelectedPath

			Directory.CreateDirectory(ArchiveFolderBase & "\" & SourcesPath)
			Directory.CreateDirectory(ArchiveFolderBase & "\" & ExtractionsPath)
			Directory.CreateDirectory(ArchiveFolderBase & "\" & ZipArchivePath)

			MessageBox.Show("Working Directories Created")

			'These are the full pathnames of all the relevant folders - this ensures that 'Relative' paths are not assumed/used
			SourcesFolder = ArchiveFolderBase & "\" & SourcesPath & "\"
			ExtractionsFolder = ArchiveFolderBase & "\" & ExtractionsPath & "\"
			ZipArchiveFolder = ArchiveFolderBase & "\" & ZipArchivePath & "\"

		End If


	End Sub

	Private Sub SelectFilesButton_Click(sender As Object, e As EventArgs) Handles SelectFilesButton.Click

		If ArchiveFolderBase.Trim <> "" Then
			With OpenFileDialog1
				.Multiselect = True
				If .ShowDialog = Windows.Forms.DialogResult.OK Then
					For Each fn As String In .FileNames
						Dim fi As New FileInfo(fn)
						My.Computer.FileSystem.CopyFile(fn, SourcesFolder & fi.Name, True)
						fi = Nothing
					Next
					MessageBox.Show("Files Copied")
				End If
			End With
		End If

	End Sub

	Private Sub CreateArchiveButton_Click(sender As Object, e As EventArgs) Handles CreateArchiveButton.Click


		ZipFile.CreateFromDirectory(SourcesFolder, ZipArchiveFolder & "MyZipFile.Zip")
		MessageBox.Show("Zip File Created")


	End Sub

	Private Sub ExtractFromArchiveButton_Click(sender As Object, e As EventArgs) Handles ExtractFromArchiveButton.Click

		ZipFile.ExtractToDirectory(ZipArchiveFolder & "MyZipFile.Zip", ExtractionsFolder)
		MessageBox.Show("Files Extracted")

	End Sub
End Class

The above shows how to copy the files a user selects into a temporary folder and then to Zip them. When I can I'll add another button ("Add File(s)") which will enable the user to add files one at a time or in a loop.
 
Hi @Softhemec!

Thank you in advance I'll test it now!
 
I don't get program to work strange.
What will i do with

Code:
 Private SourcesFolder As String = ""
    Private ExtractionsFolder As String = ""
    Private ZipArchiveFolder As String = ""

will I set some paths there?

I tested that but I didn't got any result or is it something i have missed?

2015_12_23_09_25_28_Test_Microsoft_Visual_Stud.png


I also used exactly the same code as you


Code:
Imports System.IO
Imports System.IO.Compression

Public Class Form1

    'To create the various folders
    Private ArchiveFolderBase As String = ""
    Private SourcesPath As String = "Sources"
    Private ExtractionsPath As String = "Extractions"
    Private ZipArchivePath As String = "ZipArchive"

    'These will have the full pathnames of all the relevant folders - this ensures that 'Relative' paths are not assumed/used
    Private SourcesFolder As String = ""
    Private ExtractionsFolder As String = ""
    Private ZipArchiveFolder As String = ""

    Private Sub SelectFolder_Click(ByVal sender As Object, ByVal e As EventArgs)

        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            ArchiveFolderBase = FolderBrowserDialog1.SelectedPath

            Directory.CreateDirectory(ArchiveFolderBase & "\" & SourcesPath)
            Directory.CreateDirectory(ArchiveFolderBase & "\" & ExtractionsPath)
            Directory.CreateDirectory(ArchiveFolderBase & "\" & ZipArchivePath)

            MessageBox.Show("Working Directories Created")

            'These are the full pathnames of all the relevant folders - this ensures that 'Relative' paths are not assumed/used
            SourcesFolder = ArchiveFolderBase & "\" & SourcesPath & "\"
            ExtractionsFolder = ArchiveFolderBase & "\" & ExtractionsPath & "\"
            ZipArchiveFolder = ArchiveFolderBase & "\" & ZipArchivePath & "\"

        End If

    End Sub

    Private Sub SelectFilesButton_Click(ByVal sender As Object, ByVal e As EventArgs)

        If ArchiveFolderBase.Trim <> "" Then
            With OpenFileDialog1
                .Multiselect = True
                If .ShowDialog = Windows.Forms.DialogResult.OK Then
                    For Each fn As String In .FileNames
                        Dim fi As New FileInfo(fn)
                        My.Computer.FileSystem.CopyFile(fn, SourcesFolder & fi.Name, True)
                        fi = Nothing
                    Next
                    MessageBox.Show("Files Copied")
                End If
            End With
        End If
    End Sub

    Private Sub CreateArchiveButton_Click(ByVal sender As Object, ByVal e As EventArgs)
        ZipFile.CreateFromDirectory(SourcesFolder, ZipArchiveFolder & "MyZipFile.Zip")
        MessageBox.Show("Zip File Created")
    End Sub

    Private Sub ExtractFromArchiveButton_Click(ByVal sender As Object, ByVal e As EventArgs)
        ZipFile.ExtractToDirectory(ZipArchiveFolder & "MyZipFile.Zip", ExtractionsFolder)
        MessageBox.Show("Files Extracted")
    End Sub
End Class

What is wrong?
I have no code errors either.

Thank you in advance
 
I managed to steal a couple of hours away from the kids yesterday. And I think I'll be given some time off later this afternoon so I'll try to put something together more specific to your query, but at least the above should give you a starting point.
 
Woha very fast respons :) @softhemc.
Cool

Thank you in advance.

I will test some of my own of course too I want to make a solution.
 
You must select (or create an empty folder) in the Select Folder dialog.
Once that has been done the subfolders will automatically be created.

Then when you press the OpenFile button you must select at least one file (or more if you like as MultiSelect is set to True). You can select files from any folder on your system.

I've spent the last half hour looking for a simple way to extract just the numbers from your filenames - if I can stay escaped!!! for a bit longer I should be able to post that a bit later.
 
I still don't get your code to work.

Could you send a ziparchive of your example project?

Thank you in advance
 
Hi again,

Softhemc,
Could you post the code?
Thank you in advance.
 
>a simple way to extract just the numbers from your filenames

Code:
[blue]Imports System.Text.RegularExpressions
Module UtilFunctions
    Public Function ExtractDigits(strSource As String) As String
        With New Regex("\D")
            ExtractDigits = .Replace(strSource, "")
        End With
    End Function
End Module[/blue]
 
I've had some problems with my machine. Everything is up and running now so I'll be able to post later today.
 
A bit later than planned.

Code:
Imports System.IO
Imports System.IO.Compression
Imports System.Text
Imports System.Text.RegularExpressions

Module ElmnasModule

	Public Function GetNumbers(Source As String) As String

		Return New Regex("[^\d]").Replace(Source, "")

	End Function

	Public Function ExtractFiles(SourceFile As String, TargetFolder As String, BaseFolder As String) As Boolean

		'This makes sure that the folder parts each end with "\"
		ZipFile.ExtractToDirectory(SourceFile, BaseFolder & If(BaseFolder.EndsWith("\"), "", "\") & TargetFolder & If(TargetFolder.EndsWith("\"), "", "\"))
		Return True

	End Function

End Module

Code:
	Private Sub RunButton_Click(sender As Object, e As EventArgs) Handles RunButton.Click


		'Assign these two folders as appropriate
		Dim SourceFolder As String '= The Name Of The Folder That Contains The Zipped Files
		Dim TargetParentFolder As String '= The Name Of The Folder That Is The Parent Of The Extraction Folder

		For Each fn As String In Directory.GetFiles(SourceFolder, "*.zip")
			ElmnasModule.ExtractFiles(fn, ElmnasModule.GetNumbers(fn), TargetParentFolder)
		Next

		For Each fn As String In Directory.GetFiles(SourceFolder, "*.zip")
			For Each f As String In Directory.GetFiles(ElmnasModule.GetNumbers(fn), "*.*")

				'"f" will be the name of each file that was extracted from the above code
				'Here you can process each of the extracted files as you require

			Next
		Next

	End Sub

The first block is a Module with a couple of functions:
GetNumbers uses a Regular Expression that basically says replace every character that is not a number with an empty string and return whats left. I haven't used Regular Expressions for a number of years and had forgotten that "\D" is the same as "[^\d]". You can use either pattern.

ExtractFiles makes sure that each of the elements making up the Target Directory is properly formed and then extracts the files from the Source Zip File into that folder.


The second block gives an example of how to use the code in the Module. Please note that as a Module is by default Public you don't need to use Elmnas.. However it is good practice to do so in case you have other functions/subs with the same name.


By the way I haven't included any error handling. The zip routine should definitely have some.

Additionally you mention XML in the heading but not in your question.

If you have any quetions on any of this I will be around after 10.00/10:30 is UK time.
 
Because I removed the paths that I was using, I think I've made a mistake in the code for the third For Each loop.

I think that it should be:
[tt]BaseFolder & If(BaseFolder.EndsWith("\"), "", "\") & TargetFolder & If(TargetFolder.EndsWith("\"), "", "\") & ElmnasModule.GetNumbers(fn) & "\"[/tt]

Sorry about that - that's copying and pasting and editing here.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top