Skip to content

Commit

Permalink
fix: handle __MACOSX directories in zip files
Browse files Browse the repository at this point in the history
  • Loading branch information
KoduIsGreat authored and twpayne committed Jun 22, 2024
1 parent ec9a2d9 commit e066f98
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ func NewScannerFromZipReader(zipReader *zip.Reader, options *ReadShapefileOption
var shxFiles []*zip.File
var shpFiles []*zip.File
for _, zipFile := range zipReader.File {
if isMacOSXPath(zipFile.Name) {
continue
}
switch strings.ToLower(path.Ext(zipFile.Name)) {
case ".dbf":
dbfFiles = append(dbfFiles, zipFile)
Expand Down
3 changes: 3 additions & 0 deletions shapefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ func ReadZipReader(zipReader *zip.Reader, options *ReadShapefileOptions) (*Shape
var shxFiles []*zip.File
var shpFiles []*zip.File
for _, zipFile := range zipReader.File {
if isMacOSXPath(zipFile.Name) {
continue
}
switch strings.ToLower(filepath.Ext(zipFile.Name)) {
case ".dbf":
dbfFiles = append(dbfFiles, zipFile)
Expand Down
17 changes: 17 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package shapefile

import (
"path/filepath"
"strings"
)

func isMacOSXPath(p string) bool {
dir, _ := filepath.Split(p)
pathElements := strings.Split(dir, string(filepath.Separator))
for _, elem := range pathElements {
if elem == "__MACOSX" {
return true
}
}
return false
}
29 changes: 29 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package shapefile

import (
"testing"

"github.com/alecthomas/assert/v2"
)

func TestIsMACOSX(t *testing.T) {
type testCase struct {
path string
expected bool
}
testCases := []testCase{
{"__MACOSX/dir/._test.shp", true},
{"dir/__MACOSX/._test.shp", true},
{"dir/__MACOSX/dir/._test.shp", true},
{"dir/__MACOSX/dir/__MACOSX/._test.shp", true},
{"dir/._test.shp", false},
{"dir/ABC__MACOSX", false},
{"dir/ABC__MACOSX/._test.shp", false},
{"dir/._test.shp.__MACOSX", false},
}
for _, tc := range testCases {
t.Run(tc.path, func(t *testing.T) {
assert.Equal(t, tc.expected, isMacOSXPath(tc.path))
})
}
}

0 comments on commit e066f98

Please sign in to comment.