@@ -4,6 +4,7 @@ package zipx
44
55import (
66 "archive/zip"
7+ "errors"
78 "fmt"
89 "io"
910 "os"
@@ -33,12 +34,20 @@ func DefaultPolicy() Policy {
3334
3435// Validate checks that zipPath is a readable ZIP file.
3536// Returns io.ErrUnexpectedEOF if it is not.
36- func Validate (zipPath string ) error {
37+ func Validate (zipPath string ) ( err error ) {
3738 zr , err := zip .OpenReader (zipPath )
3839 if err != nil {
39- return fmt .Errorf ("zip validate: %w" , io .ErrUnexpectedEOF )
40+ if errors .Is (err , zip .ErrFormat ) || errors .Is (err , io .ErrUnexpectedEOF ) {
41+ return fmt .Errorf ("zip validate: %w" , io .ErrUnexpectedEOF )
42+ }
43+ return fmt .Errorf ("zip validate open: %w" , err )
4044 }
41- zr .Close ()
45+ defer func () {
46+ if cerr := zr .Close (); err == nil && cerr != nil {
47+ err = fmt .Errorf ("zip validate close: %w" , cerr )
48+ }
49+ }()
50+
4251 return nil
4352}
4453
@@ -49,7 +58,12 @@ func Unzip(srcZip, destDir string, p Policy) error {
4958 if err != nil {
5059 return err
5160 }
52- defer r .Close ()
61+
62+ defer func () {
63+ if cerr := r .Close (); cerr != nil {
64+ err = errors .Join (err , fmt .Errorf ("close zip: %w" , cerr ))
65+ }
66+ }()
5367
5468 if err := os .MkdirAll (destDir , 0o755 ); err != nil {
5569 return err
@@ -122,7 +136,7 @@ func Unzip(srcZip, destDir string, p Policy) error {
122136
123137 out , err := os .OpenFile (targetAbs , os .O_CREATE | os .O_TRUNC | os .O_WRONLY , perm )
124138 if err != nil {
125- rc .Close ()
139+ _ = rc .Close ()
126140 return err
127141 }
128142
0 commit comments