-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
521caac
commit 4adbc46
Showing
5 changed files
with
115 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package filex | ||
|
||
import ( | ||
"archive/zip" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func compress(src string, dest string) error { | ||
destFile, err := os.Create(dest) | ||
if err != nil { | ||
return err | ||
} | ||
defer destFile.Close() | ||
|
||
zipWriter := zip.NewWriter(destFile) | ||
defer zipWriter.Close() | ||
|
||
err = filepath.Walk(src, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
header, err := zip.FileInfoHeader(info) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
header.Name, err = filepath.Rel(src, path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if info.IsDir() { | ||
header.Name += "/" | ||
} else { | ||
header.Method = zip.Deflate | ||
} | ||
|
||
writer, err := zipWriter.CreateHeader(header) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !info.IsDir() { | ||
file, err := os.Open(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
_, err = io.Copy(writer, file) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
return err | ||
} | ||
|
||
func extract(src string, dest string) error { | ||
zipReader, err := zip.OpenReader(src) | ||
if err != nil { | ||
return err | ||
} | ||
defer zipReader.Close() | ||
|
||
for _, file := range zipReader.File { | ||
filePath := filepath.Join(dest, file.Name) | ||
|
||
if file.FileInfo().IsDir() { | ||
os.MkdirAll(filePath, 0755) | ||
continue | ||
} | ||
|
||
if err := os.MkdirAll(filepath.Dir(filePath), 0755); err != nil { | ||
return err | ||
} | ||
|
||
inFile, err := file.Open() | ||
if err != nil { | ||
return err | ||
} | ||
defer inFile.Close() | ||
|
||
outFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode()) | ||
if err != nil { | ||
return err | ||
} | ||
defer outFile.Close() | ||
|
||
_, err = io.Copy(outFile, inFile) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters