222
schangxiang@126.com
2025-06-13 6a8393408d8cefcea02b7a598967de8dc1e565c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var fs = require('fs')
var path = require('path')
var rimraf = require('rimraf')
var test = require('tape')
var zip = require('../')
 
var filePath = path.join(__dirname, 'content', 'file.txt')
var fileZipPath = path.join(__dirname, 'content', 'file.txt.zip')
var tmpPath = path.join(__dirname, 'tmp')
 
fs.mkdirSync(tmpPath, { recursive: true })
 
test('unzipSync', function (t) {
  var tmpFilePath = path.join(tmpPath, 'file.txt')
  rimraf.sync(tmpFilePath)
  zip.unzipSync(fileZipPath, tmpPath)
 
  var tmpFile = fs.readFileSync(tmpFilePath)
  var file = fs.readFileSync(filePath)
 
  t.deepEqual(tmpFile, file)
  t.end()
})
 
test('unzip', function (t) {
  t.plan(3)
 
  var tmpFilePath = path.join(tmpPath, 'file.txt')
  rimraf(tmpFilePath, function (err) {
    t.error(err)
 
    zip.unzip(fileZipPath, tmpPath, function (err) {
      t.error(err)
 
      var tmpFile = fs.readFileSync(tmpFilePath)
      var file = fs.readFileSync(filePath)
 
      t.deepEqual(tmpFile, file)
    })
  })
})
 
test('unzip from a folder with a space in it', function (t) {
  t.plan(4)
 
  var zipSpacePath = path.join(tmpPath, 'folder space', path.basename(fileZipPath))
  fs.mkdirSync(path.dirname(zipSpacePath), { recursive: true })
  fs.copyFileSync(fileZipPath, zipSpacePath)
 
  var tmpFilePath = path.join(tmpPath, 'file.txt')
  rimraf(tmpFilePath, function (err) {
    t.error(err)
 
    zip.unzip(zipSpacePath, tmpPath, function (err) {
      t.error(err)
 
      t.ok(fs.existsSync(tmpFilePath), 'extracted file should exist')
      var tmpFile = fs.readFileSync(tmpFilePath)
      var file = fs.readFileSync(filePath)
 
      t.deepEqual(tmpFile, file)
    })
  })
})