【问题标题】:How do I create a zip folder of multiple files with the same filename, but that have different file extensions如何创建具有相同文件名但具有不同文件扩展名的多个文件的 zip 文件夹
【发布时间】:2020-02-24 18:49:48
【问题描述】:

我需要创建包含多个文件的 zip 文件夹,这些文件具有相同的文件名,但文件扩展名不同。例如,在一个文件夹中,我将拥有:

  • 字段 1.shp
  • 字段 1.shx
  • 字段 1.prj
  • 字段 1.dbf
  • 字段 2.shp
  • 字段 2.shx
  • 字段 2.prj
  • 字段 2.dbf

等等。

我需要将所有字段 1 文件一起压缩到字段 1.zip 中,字段 2 文件一起压缩到字段 2.zip 等中,循环遍历文件夹。

我现在在这里:

    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

我添加了“dotnetzip”和导入的 Ionic.zip。上面的代码将分别压缩每个 .dbf、.prj、.shp 和 .shx 文件,但我对如何将每个 4 个文件合并为一个 zip 文件感到困惑。

提前谢谢你。

【问题讨论】:

标签: vb.net


【解决方案1】:

这是我通过之前通过 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

这会将我需要的每个文件部分(.dbf、.prj、.shp、.shx)与一个匹配的文件名一起压缩到一个 filename.zip 文件中。

【讨论】:

    猜你喜欢
    • 2016-06-30
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-18
    相关资源
    最近更新 更多