INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

how to find an XML somewhere in a zipfile or subzips

how to find an XML somewhere in a zipfile or subzips

(OP)
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 --> VB.net

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

RE: how to find an XML somewhere in a zipfile or subzips

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.

RE: how to find an XML somewhere in a zipfile or subzips

(OP)
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..

RE: how to find an XML somewhere in a zipfile or subzips

I'll try to put an example together over the weekend.

RE: how to find an XML somewhere in a zipfile or subzips

(OP)
Hi @Softhemc,

Thank you in advance!

RE: how to find an XML somewhere in a zipfile or subzips

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.

RE: how to find an XML somewhere in a zipfile or subzips

(OP)
haahah aww kids :)

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

Thank you in advance mate.

RE: how to find an XML somewhere in a zipfile or subzips

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 --> vb

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.

RE: how to find an XML somewhere in a zipfile or subzips

(OP)
Hi @Softhemec!

Thank you in advance I'll test it now!

RE: how to find an XML somewhere in a zipfile or subzips

(OP)
I don't get program to work strange.
What will i do with

CODE --> VB.net

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?



I also used exactly the same code as you


CODE --> VB.net

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


RE: how to find an XML somewhere in a zipfile or subzips

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.

RE: how to find an XML somewhere in a zipfile or subzips

(OP)
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.

RE: how to find an XML somewhere in a zipfile or subzips

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.

RE: how to find an XML somewhere in a zipfile or subzips

(OP)
I still don't get your code to work.

Could you send a ziparchive of your example project?

Thank you in advance

RE: how to find an XML somewhere in a zipfile or subzips

I'll post tomorrow

RE: how to find an XML somewhere in a zipfile or subzips

(OP)
Hi again,

Softhemc,
Could you post the code?
Thank you in advance.

RE: how to find an XML somewhere in a zipfile or subzips

>a simple way to extract just the numbers from your filenames

CODE

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 

RE: how to find an XML somewhere in a zipfile or subzips

I've had some problems with my machine. Everything is up and running now so I'll be able to post later today.

RE: how to find an XML somewhere in a zipfile or subzips

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.

RE: how to find an XML somewhere in a zipfile or subzips

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:
BaseFolder & If(BaseFolder.EndsWith("\"), "", "\") & TargetFolder & If(TargetFolder.EndsWith("\"), "", "\") & ElmnasModule.GetNumbers(fn) & "\"

Sorry about that - that's copying and pasting and editing here.

RE: how to find an XML somewhere in a zipfile or subzips

I won't be around now for the next couple of hours. When you've had chance to look at this let me know how you get on.

RE: how to find an XML somewhere in a zipfile or subzips

(OP)
Thank you man awesome!! I'll try it out then!

RE: how to find an XML somewhere in a zipfile or subzips

Corrections to my previous post.

I've created some folders to make it clearer:
C:\TTTesting
C:\TTTesting\ElmnasTestFolder
C:\TTTesting\ElmnasTestFolder\UnzippedTargetFiles
C:\TTTesting\ElmnasTestFolder\ZippedSourceFiles

I placed a Zip file in C:\TTTesting\ElmnasTestFolder\ZippedSourceFiles so that I could test everything and to avoid the confusion my earlier post probably caused I've used these folders in the code - so that all you will need to do is change them to your requirements.

The Module:

CODE

Imports System.IO
Imports System.IO.Compression
Imports System.Text
Imports System.Text.RegularExpressions

Module ElmnasModule


	'Pattern
	Private GetNumbersPattern As String = "[^\d]"

	Public Function GetNumbers(Source As String) As String

		Return New Regex(GetNumbersPattern).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 

Sample code - the Form contains two Buttons and a ListBox:

CODE

Imports System.IO

Public Class Form2

	Private Sub CloseButton_Click(sender As Object, e As EventArgs) Handles CloseButton.Click

		Close()

	End Sub



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

		RunButton.Enabled = False

		'Assign these two folders as appropriate

		'Dim SourceFolder As String '= The Name Of The Folder That Contains The Zipped Files
		Dim SourceFolder As String = "C:\TTTesting\ElmnasTestFolder\ZippedSourceFiles"

		'Dim TargetParentFolder As String '= The Name Of The Folder That Is The Parent Of The Extraction Folder
		Dim TargetParentFolder As String = "C:\TTTesting\ElmnasTestFolder\UnzippedTargetFiles"


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

		'As an example - display the list the extracted files in a list box
		ListBox1.Items.Clear()

		For Each fn As String In Directory.GetFiles(SourceFolder, "*.zip")
			For Each f As String In Directory.GetFiles("C:\TTTesting\ElmnasTestFolder\UnzippedTargetFiles\" & 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

				ListBox1.Items.Add(f)

			Next
		Next

		RunButton.Enabled = True

	End Sub

End Class 

RE: how to find an XML somewhere in a zipfile or subzips

(OP)
Thank you sir I will take a look on your master code when I am finished with this :)

Awesome!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close