schangxiang@126.com
2025-05-21 912ebf022f5aff755971341c555726fa6ac5496d
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict'
Object.defineProperty(exports, '__esModule', { value: true })
exports.tar =
  exports.zip =
  exports.ZipAFolder =
  exports.COMPRESSION_LEVEL =
    void 0
const tslib_1 = require('tslib')
const fs_1 = require('fs')
const path_1 = tslib_1.__importDefault(require('path'))
const archiver_1 = tslib_1.__importDefault(require('archiver'))
const promises_1 = tslib_1.__importDefault(require('fs/promises'))
const is_glob_1 = tslib_1.__importDefault(require('is-glob'))
const glob_1 = require('glob')
var COMPRESSION_LEVEL
;(function (COMPRESSION_LEVEL) {
  COMPRESSION_LEVEL[(COMPRESSION_LEVEL['uncompressed'] = 0)] = 'uncompressed'
  COMPRESSION_LEVEL[(COMPRESSION_LEVEL['medium'] = 5)] = 'medium'
  COMPRESSION_LEVEL[(COMPRESSION_LEVEL['high'] = 9)] = 'high'
})(COMPRESSION_LEVEL || (exports.COMPRESSION_LEVEL = COMPRESSION_LEVEL = {}))
class ZipAFolder {
  static tar(src, tarFilePath, zipAFolderOptions) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
      const o = zipAFolderOptions || {
        compression: COMPRESSION_LEVEL.high,
      }
      if (o.compression === COMPRESSION_LEVEL.uncompressed) {
        yield ZipAFolder.compress({
          src,
          targetFilePath: tarFilePath,
          format: 'tar',
          zipAFolderOptions,
        })
      } else {
        yield ZipAFolder.compress({
          src,
          targetFilePath: tarFilePath,
          format: 'tar',
          zipAFolderOptions,
          archiverOptions: {
            gzip: true,
            gzipOptions: {
              level: o.compression,
            },
          },
        })
      }
    })
  }
  static zip(src, zipFilePath, zipAFolderOptions) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
      const o = zipAFolderOptions || {
        compression: COMPRESSION_LEVEL.high,
      }
      if (o.compression === COMPRESSION_LEVEL.uncompressed) {
        yield ZipAFolder.compress({
          src,
          targetFilePath: zipFilePath,
          format: 'zip',
          zipAFolderOptions,
          archiverOptions: {
            store: true,
          },
        })
      } else {
        yield ZipAFolder.compress({
          src,
          targetFilePath: zipFilePath,
          format: 'zip',
          zipAFolderOptions,
          archiverOptions: {
            zlib: {
              level: o.compression,
            },
          },
        })
      }
    })
  }
  static compress(_a) {
    return tslib_1.__awaiter(
      this,
      arguments,
      void 0,
      function* ({
        src,
        targetFilePath,
        format,
        zipAFolderOptions,
        archiverOptions,
      }) {
        let output
        const globList = []
        if (
          !(zipAFolderOptions === null || zipAFolderOptions === void 0
            ? void 0
            : zipAFolderOptions.customWriteStream) &&
          targetFilePath
        ) {
          const targetBasePath = path_1.default.dirname(targetFilePath)
          if (targetBasePath === src) {
            throw new Error('Source and target folder must be different.')
          }
          try {
            if (!(0, is_glob_1.default)(src)) {
              yield promises_1.default.access(
                src,
                promises_1.default.constants.R_OK
              )
            }
            yield promises_1.default.access(
              targetBasePath,
              promises_1.default.constants.R_OK |
                promises_1.default.constants.W_OK
            )
          } catch (e) {
            throw new Error(`Permission error: ${e.message}`)
          }
          if ((0, is_glob_1.default)(src)) {
            for (const globPart of src.split(',')) {
              globList.push(...(yield (0, glob_1.glob)(globPart.trim())))
            }
            if (globList.length === 0) {
              throw new Error(`No glob match found for "${src}".`)
            }
          }
          output = (0, fs_1.createWriteStream)(targetFilePath)
        } else if (zipAFolderOptions && zipAFolderOptions.customWriteStream) {
          output = zipAFolderOptions.customWriteStream
        } else {
          throw new Error(
            'You must either provide a target file path or a custom write stream to write to.'
          )
        }
        const zipArchive = (0, archiver_1.default)(
          format,
          archiverOptions || {}
        )
        return new Promise((resolve, reject) =>
          tslib_1.__awaiter(this, void 0, void 0, function* () {
            output.on('close', resolve)
            output.on('error', reject)
            zipArchive.pipe(output)
            if ((0, is_glob_1.default)(src)) {
              for (const file of globList) {
                if ((yield promises_1.default.lstat(file)).isFile()) {
                  const content = yield promises_1.default.readFile(file)
                  zipArchive.append(content, {
                    name: file,
                  })
                }
              }
            } else {
              zipArchive.directory(
                src,
                (zipAFolderOptions === null || zipAFolderOptions === void 0
                  ? void 0
                  : zipAFolderOptions.destPath) || false,
                {
                  date: new Date(Date.now() + 8 * 60 * 60 * 1000),
                }
              )
            }
            yield zipArchive.finalize()
          })
        )
      }
    )
  }
}
exports.ZipAFolder = ZipAFolder
exports.zip = ZipAFolder.zip
exports.tar = ZipAFolder.tar
//# sourceMappingURL=ZipAFolder.js.map