zip_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package fileutil
  2. import (
  3. "encoding/base64"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "strings"
  8. "testing"
  9. )
  10. var testZipFile = "UEsDBBQAAAAAAAlhM0sAAAAAAAAAAAAAAAALAAAAdGVzdGZvbGRlci" +
  11. "9QSwMECgAAAAAA/WAzS9JjSIgDAAAAAwAAABQAAAB0ZXN0Zm9sZGVyL3Rlc3QxLnR4dDEyM1" +
  12. "BLAwQKAAAAAAAMYTNLccOosQMAAAADAAAAFAAAAHRlc3Rmb2xkZXIvdGVzdDIudHh0NDU2UE" +
  13. "sBAj8AFAAAAAAACWEzSwAAAAAAAAAAAAAAAAsAJAAAAAAAAAAQAAAAAAAAAHRlc3Rmb2xkZX" +
  14. "IvCgAgAAAAAAABABgAynC8mDcx0wG6nMOYNzHTAcpwvJg3MdMBUEsBAj8ACgAAAAAA/WAzS9" +
  15. "JjSIgDAAAAAwAAABQAJAAAAAAAAAAgAAAAKQAAAHRlc3Rmb2xkZXIvdGVzdDEudHh0CgAgAA" +
  16. "AAAAABABgAAgkxjDcx0wFqBhKVNzHTAQIJMYw3MdMBUEsBAj8ACgAAAAAADGEzS3HDqLEDAA" +
  17. "AAAwAAABQAJAAAAAAAAAAgAAAAXgAAAHRlc3Rmb2xkZXIvdGVzdDIudHh0CgAgAAAAAAABAB" +
  18. "gArtRMnDcx0wE68M6gNzHTAXrDTJw3MdMBUEsFBgAAAAADAAMAKQEAAJMAAAAAAA=="
  19. func TestUnzipFile(t *testing.T) {
  20. data, _ := base64.StdEncoding.DecodeString(testZipFile)
  21. ioutil.WriteFile("ziptest.zip", data, 0660)
  22. ioutil.WriteFile("ziptest2.zip", data[:5], 0660)
  23. defer func() {
  24. os.Remove("ziptest.zip")
  25. os.Remove("ziptest2.zip")
  26. os.RemoveAll("foo")
  27. }()
  28. if err := UnzipFile("ziptest.zip", "foo", false); err != nil {
  29. t.Error(err)
  30. return
  31. }
  32. if err := UnzipFile("ziptest.zip", "foo", false); !strings.Contains(err.Error(), "Path already exists:") {
  33. t.Error(err)
  34. return
  35. }
  36. if err := UnzipFile("ziptest2.zip", "foo", false); err.Error() != "zip: not a valid zip file" {
  37. t.Error(err)
  38. return
  39. }
  40. if e, err := PathExists("foo"); !e {
  41. t.Error("Unexpected result:", e, err)
  42. return
  43. }
  44. if e, err := PathExists(path.Join("foo", "testfolder")); !e {
  45. t.Error("Unexpected result:", e, err)
  46. return
  47. }
  48. if e, err := PathExists(path.Join("foo", "testfolder", "test1.txt")); !e {
  49. t.Error("Unexpected result:", e, err)
  50. return
  51. }
  52. if e, err := PathExists(path.Join("foo", "testfolder", "test2.txt")); !e {
  53. t.Error("Unexpected result:", e, err)
  54. return
  55. }
  56. if c, err := ioutil.ReadFile(path.Join("foo", "testfolder", "test1.txt")); string(c) != "123" {
  57. t.Error("Unexpected result:", string(c), err)
  58. return
  59. }
  60. if c, err := ioutil.ReadFile(path.Join("foo", "testfolder", "test2.txt")); string(c) != "456" {
  61. t.Error("Unexpected result:", string(c), err)
  62. return
  63. }
  64. }