File size: 4,853 Bytes
765fe0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use strict';
// A small streaming ZIP writer (stored entries, no compression, no ZIP64):
// folder downloads and voice-dataset exports. Media and archives are already
// compressed and text compresses on the wire anyway, so "store" keeps it
// simple and fast. Limits: < 4 GB in total and < 65535 entries.

const fs = require('fs');

const CRC_TABLE = (() => {
  const table = new Uint32Array(256);
  for (let n = 0; n < 256; n += 1) {
    let c = n;
    for (let k = 0; k < 8; k += 1) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
    table[n] = c >>> 0;
  }
  return table;
})();

function crc32Update(crc, buf) {
  let c = crc ^ 0xffffffff;
  for (let i = 0; i < buf.length; i += 1) c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8);
  return (c ^ 0xffffffff) >>> 0;
}

function dosTime(date) {
  const d = date instanceof Date ? date : new Date();
  const time = (d.getHours() << 11) | (d.getMinutes() << 5) | Math.floor(d.getSeconds() / 2);
  const day = ((Math.max(1980, d.getFullYear()) - 1980) << 9) | ((d.getMonth() + 1) << 5) | d.getDate();
  return { time, day };
}

class ZipWriter {
  // out: a writable stream (an HTTP response).
  constructor(out) {
    this.out = out;
    this.offset = 0;
    this.entries = [];
  }

  write(buf) {
    this.offset += buf.length;
    return new Promise((resolve, reject) => {
      if (this.out.destroyed) {
        reject(new Error('bağlantı kapandı'));
        return;
      }
      if (this.out.write(buf)) resolve();
      else {
        const onDrain = () => {
          this.out.off('close', onClose);
          resolve();
        };
        const onClose = () => {
          this.out.off('drain', onDrain);
          reject(new Error('bağlantı kapandı'));
        };
        this.out.once('drain', onDrain);
        this.out.once('close', onClose);
      }
    });
  }

  async addEntry(name, source, mtime) {
    if (this.entries.length >= 65534) throw new Error('çok fazla dosya');
    const nameBuf = Buffer.from(name.replace(/\\/g, '/'), 'utf8');
    const { time, day } = dosTime(mtime);
    const header = Buffer.alloc(30);
    header.writeUInt32LE(0x04034b50, 0);
    header.writeUInt16LE(20, 4); // version needed
    header.writeUInt16LE(0x0808, 6); // data descriptor + UTF-8 names
    header.writeUInt16LE(0, 8); // stored
    header.writeUInt16LE(time, 10);
    header.writeUInt16LE(day, 12);
    header.writeUInt32LE(0, 14);
    header.writeUInt32LE(0, 18);
    header.writeUInt32LE(0, 22);
    header.writeUInt16LE(nameBuf.length, 26);
    header.writeUInt16LE(0, 28);
    const start = this.offset;
    await this.write(header);
    await this.write(nameBuf);
    let crc = 0;
    let size = 0;
    if (Buffer.isBuffer(source)) {
      crc = crc32Update(0, source);
      size = source.length;
      if (size) await this.write(source);
    } else {
      for await (const chunk of fs.createReadStream(source)) {
        crc = crc32Update(crc, chunk);
        size += chunk.length;
        await this.write(chunk);
      }
    }
    if (this.offset > 0xfffffff0) throw new Error('arşiv 4 GB sınırını aşıyor');
    const desc = Buffer.alloc(16);
    desc.writeUInt32LE(0x08074b50, 0);
    desc.writeUInt32LE(crc, 4);
    desc.writeUInt32LE(size, 8);
    desc.writeUInt32LE(size, 12);
    await this.write(desc);
    this.entries.push({ nameBuf, crc, size, time, day, offset: start });
  }

  addFile(name, file, mtime) {
    return this.addEntry(name, file, mtime);
  }

  addBuffer(name, buf, mtime) {
    return this.addEntry(name, Buffer.isBuffer(buf) ? buf : Buffer.from(String(buf), 'utf8'), mtime);
  }

  async finish() {
    const start = this.offset;
    for (const e of this.entries) {
      const h = Buffer.alloc(46);
      h.writeUInt32LE(0x02014b50, 0);
      h.writeUInt16LE(0x0314, 4); // made by: unix, 2.0
      h.writeUInt16LE(20, 6);
      h.writeUInt16LE(0x0808, 8);
      h.writeUInt16LE(0, 10);
      h.writeUInt16LE(e.time, 12);
      h.writeUInt16LE(e.day, 14);
      h.writeUInt32LE(e.crc, 16);
      h.writeUInt32LE(e.size, 20);
      h.writeUInt32LE(e.size, 24);
      h.writeUInt16LE(e.nameBuf.length, 28);
      h.writeUInt16LE(0, 30);
      h.writeUInt16LE(0, 32);
      h.writeUInt16LE(0, 34);
      h.writeUInt16LE(0, 36);
      h.writeUInt32LE((0o100644 << 16) >>> 0, 38);
      h.writeUInt32LE(e.offset, 42);
      await this.write(h);
      await this.write(e.nameBuf);
    }
    const size = this.offset - start;
    const end = Buffer.alloc(22);
    end.writeUInt32LE(0x06054b50, 0);
    end.writeUInt16LE(0, 4);
    end.writeUInt16LE(0, 6);
    end.writeUInt16LE(this.entries.length, 8);
    end.writeUInt16LE(this.entries.length, 10);
    end.writeUInt32LE(size, 12);
    end.writeUInt32LE(start, 16);
    end.writeUInt16LE(0, 20);
    await this.write(end);
    this.out.end();
  }
}

module.exports = { ZipWriter, crc32Update };