0

I need to create zip folders containing multiple files that will have the same filename, but different file extension. For example, in one folder I will have:

  • Field 1.shp
  • Field 1.shx
  • Field 1.prj
  • Field 1.dbf
  • Field 2.shp
  • Field 2.shx
  • Field 2.prj
  • Field 2.dbf

etc..

I need to zip all Field 1 files together in Field 1.zip, Field 2 files together in Field 2.zip etc. looping through the folder.

Here I am currently:

    Private Sub btnZipFiles_Click(sender As Object, e As EventArgs) Handles btnZipFiles.Click
        Dim dir = SelectedPath.Text
        Dim reqextensions As String = "*.dbf,*.prj,*.shp,*.shx"
        Dim files = Directory.EnumerateFiles(dir, "*.*", SearchOption.AllDirectories)


        For Each file In files
            Using zip As New ZipFile
                zip.AddFile(file, "")
                zip.Save(file + ".zip")
            End Using
        Next
    End Sub

I've added "dotnetzip" and Imported Ionic.zip. The above code will zip each .dbf, .prj, .shp, and .shx file individually, but I am stuck on how to combine each 4 files into one zip file.

Thank you in advance.

RockiesSkier
  • 105
  • 1
  • 8
  • The command you are using `CreateFromDirectory` uses all files in a folder, you cannot filter the files using this method. If you don't want to move the files in to their own folders then you need to use a different method. Try: `ZipFileExtensions.CreateEntryFromFile` Method https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.zipfileextensions.createentryfromfile?view=netframework-4.8 – HackSlash Feb 24 '20 at 19:17
  • Does this answer your question? [How to use System.IO.Compression to read/write ZIP files?](https://stackoverflow.com/questions/184309/how-to-use-system-io-compression-to-read-write-zip-files) – HackSlash Feb 24 '20 at 19:21

1 Answers1

0

Here is what I came up with with my desired result looping through the files previously called via Directory.EnumerateFiles:

        For Each file In files
            Using zip As New ZipFile
                zip.AddFile(dir + "\" + Path.GetFileNameWithoutExtension(file.Name) + ".dbf", "")
                zip.AddFile(dir + "\" + Path.GetFileNameWithoutExtension(file.Name) + ".prj", "")
                zip.AddFile(dir + "\" + Path.GetFileNameWithoutExtension(file.Name) + ".shx", "")
                zip.AddFile(dir + "\" + Path.GetFileNameWithoutExtension(file.Name) + ".shp", "")
                zip.Save(zippath + Path.GetFileNameWithoutExtension(file.Name) + ".zip")
            End Using
        Next

This takes each file part I require (.dbf, .prj, .shp, .shx) with a matching filename and zips each together in a filename.zip file.

RockiesSkier
  • 105
  • 1
  • 8