diff --git a/.gitignore b/.gitignore index e0921c7..eb0ea1b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ .DS_Store cryptor/*.txt fileutil/*.txt +fileutil/*.zip +fileutil/unzip/* cryptor/*.pem \ No newline at end of file diff --git a/fileutil/file.go b/fileutil/file.go index 6f56bc8..994b2e2 100644 --- a/fileutil/file.go +++ b/fileutil/file.go @@ -5,11 +5,14 @@ package fileutil import ( + "archive/zip" "bufio" "errors" "io" "io/ioutil" "os" + "path/filepath" + "strings" ) // IsExist checks if a file or directory exists @@ -148,3 +151,92 @@ func ListFileNames(path string) ([]string, error) { return res, nil } + +// Zip create zip file, srcFile could be a single file or a directory +func Zip(srcFile string, destPath string) error { + zipFile, err := os.Create(destPath) + if err != nil { + return err + } + defer zipFile.Close() + + archive := zip.NewWriter(zipFile) + defer archive.Close() + + filepath.Walk(srcFile, 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 = strings.TrimPrefix(path, filepath.Dir(srcFile)+"/") + + if info.IsDir() { + header.Name += "/" + } else { + header.Method = zip.Deflate + } + + writer, err := archive.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 nil +} + +// UnZip unzip the file and save it to destPath +func UnZip(zipFile string, destPath string) error { + zipReader, err := zip.OpenReader(zipFile) + if err != nil { + return err + } + defer zipReader.Close() + + for _, f := range zipReader.File { + path := filepath.Join(destPath, f.Name) + if f.FileInfo().IsDir() { + os.MkdirAll(path, os.ModePerm) + } else { + if err = os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil { + return err + } + + inFile, err := f.Open() + if err != nil { + return err + } + defer inFile.Close() + + outFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) + if err != nil { + return err + } + defer outFile.Close() + + _, err = io.Copy(outFile, inFile) + if err != nil { + return err + } + } + } + return nil +} diff --git a/fileutil/file_test.go b/fileutil/file_test.go index 729f513..2149f0d 100644 --- a/fileutil/file_test.go +++ b/fileutil/file_test.go @@ -37,6 +37,7 @@ func TestCreateFile(t *testing.T) { internal.LogFailedTestInfo(t, "CreateFile", f, f, "create file error") t.FailNow() } + os.Remove(f) } func TestIsDir(t *testing.T) { @@ -70,20 +71,22 @@ func TestCopyFile(t *testing.T) { srcFile := "./text.txt" CreateFile(srcFile) - dstFile := "./text_copy.txt" + destFile := "./text_copy.txt" - err := CopyFile(srcFile, dstFile) + err := CopyFile(srcFile, destFile) if err != nil { - file, err := os.Open(dstFile) + file, err := os.Open(destFile) if err != nil { - internal.LogFailedTestInfo(t, "CopyFile", srcFile, dstFile, "create file error: "+err.Error()) + internal.LogFailedTestInfo(t, "CopyFile", srcFile, destFile, "create file error: "+err.Error()) t.FailNow() } - if file.Name() != dstFile { - internal.LogFailedTestInfo(t, "CopyFile", srcFile, dstFile, file.Name()) + if file.Name() != destFile { + internal.LogFailedTestInfo(t, "CopyFile", srcFile, destFile, file.Name()) t.FailNow() } } + os.Remove(srcFile) + os.Remove(destFile) } func TestListFileNames(t *testing.T) { @@ -101,32 +104,41 @@ func TestListFileNames(t *testing.T) { func TestReadFileToString(t *testing.T) { path := "./text.txt" CreateFile(path) + f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777) f.WriteString("hello world") res, _ := ReadFileToString(path) if res != "hello world" { internal.LogFailedTestInfo(t, "ReadFileToString", path, "hello world", res) + t.FailNow() } + os.Remove(path) } func TestClearFile(t *testing.T) { path := "./text.txt" CreateFile(path) + f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777) f.WriteString("hello world") - CreateFile(path) - - res, _ := ReadFileToString(path) - if res != "" { - internal.LogFailedTestInfo(t, "CreateFile", path, "", res) + err := ClearFile(path) + if err != nil { + t.Error("Clear file error: ", err) } + fileContent, _ := ReadFileToString(path) + if fileContent != "" { + internal.LogFailedTestInfo(t, "ClearFile", path, "", fileContent) + t.FailNow() + } + os.Remove(path) } func TestReadFileByLine(t *testing.T) { path := "./text.txt" CreateFile(path) + f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777) f.WriteString("hello\nworld") @@ -134,5 +146,38 @@ func TestReadFileByLine(t *testing.T) { res, _ := ReadFileByLine(path) if !reflect.DeepEqual(res, expected) { internal.LogFailedTestInfo(t, "ReadFileByLine", path, expected, res) + t.FailNow() } + os.Remove(path) +} + +func TestZipAndUnZip(t *testing.T) { + srcFile := "./text.txt" + CreateFile(srcFile) + + file, _ := os.OpenFile(srcFile, os.O_WRONLY|os.O_TRUNC, 0777) + file.WriteString("hello\nworld") + + zipFile := "./text.zip" + err := Zip(srcFile, zipFile) + if err != nil { + internal.LogFailedTestInfo(t, "Zip", srcFile, zipFile, err) + t.FailNow() + } + + unZipPath := "./unzip" + err = UnZip(zipFile, unZipPath) + if err != nil { + internal.LogFailedTestInfo(t, "UnZip", srcFile, unZipPath, err) + t.FailNow() + } + + unZipFile := "./unzip/text.txt" + if !IsExist(unZipFile) { + internal.LogFailedTestInfo(t, "UnZip", zipFile, zipFile, err) + t.FailNow() + } + os.Remove(srcFile) + os.Remove(zipFile) + os.RemoveAll(unZipPath) }