You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

unzip.js 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const fs = require('fs')
  2. const path = require('path')
  3. const test = require('tape')
  4. const zip = require('../')
  5. const filePath = path.join(__dirname, 'content', 'file.txt')
  6. const fileZipPath = path.join(__dirname, 'content', 'file.txt.zip')
  7. const tmpPath = path.join(__dirname, 'tmp')
  8. fs.mkdirSync(tmpPath, { recursive: true })
  9. test('unzipSync', function (t) {
  10. const tmpFilePath = path.join(tmpPath, 'file.txt')
  11. fs.rmSync(tmpFilePath, { recursive: true, force: true })
  12. zip.unzipSync(fileZipPath, tmpPath)
  13. const tmpFile = fs.readFileSync(tmpFilePath)
  14. const file = fs.readFileSync(filePath)
  15. t.deepEqual(tmpFile, file)
  16. t.end()
  17. })
  18. test('unzip', function (t) {
  19. t.plan(3)
  20. const tmpFilePath = path.join(tmpPath, 'file.txt')
  21. fs.rm(tmpFilePath, { recursive: true }, function (err) {
  22. t.error(err)
  23. zip.unzip(fileZipPath, tmpPath, function (err) {
  24. t.error(err)
  25. const tmpFile = fs.readFileSync(tmpFilePath)
  26. const file = fs.readFileSync(filePath)
  27. t.deepEqual(tmpFile, file)
  28. })
  29. })
  30. })
  31. test('unzip from a folder with a space in it', function (t) {
  32. t.plan(4)
  33. const zipSpacePath = path.join(
  34. tmpPath,
  35. 'folder space',
  36. path.basename(fileZipPath)
  37. )
  38. fs.mkdirSync(path.dirname(zipSpacePath), { recursive: true })
  39. fs.copyFileSync(fileZipPath, zipSpacePath)
  40. const tmpFilePath = path.join(tmpPath, 'file.txt')
  41. fs.rm(tmpFilePath, { recursive: true }, function (err) {
  42. t.error(err)
  43. zip.unzip(zipSpacePath, tmpPath, function (err) {
  44. t.error(err)
  45. t.ok(fs.existsSync(tmpFilePath), 'extracted file should exist')
  46. const tmpFile = fs.readFileSync(tmpFilePath)
  47. const file = fs.readFileSync(filePath)
  48. t.deepEqual(tmpFile, file)
  49. })
  50. })
  51. })