123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- /**
- * node-compress-commons
- *
- * Copyright (c) 2014 Chris Talkington, contributors.
- * Licensed under the MIT license.
- * https://github.com/archiverjs/node-compress-commons/blob/master/LICENSE-MIT
- */
- var inherits = require('util').inherits;
- var normalizePath = require('normalize-path');
-
- var ArchiveEntry = require('../archive-entry');
- var GeneralPurposeBit = require('./general-purpose-bit');
- var UnixStat = require('./unix-stat');
-
- var constants = require('./constants');
- var zipUtil = require('./util');
-
- var ZipArchiveEntry = module.exports = function(name) {
- if (!(this instanceof ZipArchiveEntry)) {
- return new ZipArchiveEntry(name);
- }
-
- ArchiveEntry.call(this);
-
- this.platform = constants.PLATFORM_FAT;
- this.method = -1;
-
- this.name = null;
- this.size = 0;
- this.csize = 0;
- this.gpb = new GeneralPurposeBit();
- this.crc = 0;
- this.time = -1;
-
- this.minver = constants.MIN_VERSION_INITIAL;
- this.mode = -1;
- this.extra = null;
- this.exattr = 0;
- this.inattr = 0;
- this.comment = null;
-
- if (name) {
- this.setName(name);
- }
- };
-
- inherits(ZipArchiveEntry, ArchiveEntry);
-
- /**
- * Returns the extra fields related to the entry.
- *
- * @returns {Buffer}
- */
- ZipArchiveEntry.prototype.getCentralDirectoryExtra = function() {
- return this.getExtra();
- };
-
- /**
- * Returns the comment set for the entry.
- *
- * @returns {string}
- */
- ZipArchiveEntry.prototype.getComment = function() {
- return this.comment !== null ? this.comment : '';
- };
-
- /**
- * Returns the compressed size of the entry.
- *
- * @returns {number}
- */
- ZipArchiveEntry.prototype.getCompressedSize = function() {
- return this.csize;
- };
-
- /**
- * Returns the CRC32 digest for the entry.
- *
- * @returns {number}
- */
- ZipArchiveEntry.prototype.getCrc = function() {
- return this.crc;
- };
-
- /**
- * Returns the external file attributes for the entry.
- *
- * @returns {number}
- */
- ZipArchiveEntry.prototype.getExternalAttributes = function() {
- return this.exattr;
- };
-
- /**
- * Returns the extra fields related to the entry.
- *
- * @returns {Buffer}
- */
- ZipArchiveEntry.prototype.getExtra = function() {
- return this.extra !== null ? this.extra : constants.EMPTY;
- };
-
- /**
- * Returns the general purpose bits related to the entry.
- *
- * @returns {GeneralPurposeBit}
- */
- ZipArchiveEntry.prototype.getGeneralPurposeBit = function() {
- return this.gpb;
- };
-
- /**
- * Returns the internal file attributes for the entry.
- *
- * @returns {number}
- */
- ZipArchiveEntry.prototype.getInternalAttributes = function() {
- return this.inattr;
- };
-
- /**
- * Returns the last modified date of the entry.
- *
- * @returns {number}
- */
- ZipArchiveEntry.prototype.getLastModifiedDate = function() {
- return this.getTime();
- };
-
- /**
- * Returns the extra fields related to the entry.
- *
- * @returns {Buffer}
- */
- ZipArchiveEntry.prototype.getLocalFileDataExtra = function() {
- return this.getExtra();
- };
-
- /**
- * Returns the compression method used on the entry.
- *
- * @returns {number}
- */
- ZipArchiveEntry.prototype.getMethod = function() {
- return this.method;
- };
-
- /**
- * Returns the filename of the entry.
- *
- * @returns {string}
- */
- ZipArchiveEntry.prototype.getName = function() {
- return this.name;
- };
-
- /**
- * Returns the platform on which the entry was made.
- *
- * @returns {number}
- */
- ZipArchiveEntry.prototype.getPlatform = function() {
- return this.platform;
- };
-
- /**
- * Returns the size of the entry.
- *
- * @returns {number}
- */
- ZipArchiveEntry.prototype.getSize = function() {
- return this.size;
- };
-
- /**
- * Returns a date object representing the last modified date of the entry.
- *
- * @returns {number|Date}
- */
- ZipArchiveEntry.prototype.getTime = function() {
- return this.time !== -1 ? zipUtil.dosToDate(this.time) : -1;
- };
-
- /**
- * Returns the DOS timestamp for the entry.
- *
- * @returns {number}
- */
- ZipArchiveEntry.prototype.getTimeDos = function() {
- return this.time !== -1 ? this.time : 0;
- };
-
- /**
- * Returns the UNIX file permissions for the entry.
- *
- * @returns {number}
- */
- ZipArchiveEntry.prototype.getUnixMode = function() {
- return this.platform !== constants.PLATFORM_UNIX ? 0 : ((this.getExternalAttributes() >> constants.SHORT_SHIFT) & constants.SHORT_MASK);
- };
-
- /**
- * Returns the version of ZIP needed to extract the entry.
- *
- * @returns {number}
- */
- ZipArchiveEntry.prototype.getVersionNeededToExtract = function() {
- return this.minver;
- };
-
- /**
- * Sets the comment of the entry.
- *
- * @param comment
- */
- ZipArchiveEntry.prototype.setComment = function(comment) {
- if (Buffer.byteLength(comment) !== comment.length) {
- this.getGeneralPurposeBit().useUTF8ForNames(true);
- }
-
- this.comment = comment;
- };
-
- /**
- * Sets the compressed size of the entry.
- *
- * @param size
- */
- ZipArchiveEntry.prototype.setCompressedSize = function(size) {
- if (size < 0) {
- throw new Error('invalid entry compressed size');
- }
-
- this.csize = size;
- };
-
- /**
- * Sets the checksum of the entry.
- *
- * @param crc
- */
- ZipArchiveEntry.prototype.setCrc = function(crc) {
- if (crc < 0) {
- throw new Error('invalid entry crc32');
- }
-
- this.crc = crc;
- };
-
- /**
- * Sets the external file attributes of the entry.
- *
- * @param attr
- */
- ZipArchiveEntry.prototype.setExternalAttributes = function(attr) {
- this.exattr = attr >>> 0;
- };
-
- /**
- * Sets the extra fields related to the entry.
- *
- * @param extra
- */
- ZipArchiveEntry.prototype.setExtra = function(extra) {
- this.extra = extra;
- };
-
- /**
- * Sets the general purpose bits related to the entry.
- *
- * @param gpb
- */
- ZipArchiveEntry.prototype.setGeneralPurposeBit = function(gpb) {
- if (!(gpb instanceof GeneralPurposeBit)) {
- throw new Error('invalid entry GeneralPurposeBit');
- }
-
- this.gpb = gpb;
- };
-
- /**
- * Sets the internal file attributes of the entry.
- *
- * @param attr
- */
- ZipArchiveEntry.prototype.setInternalAttributes = function(attr) {
- this.inattr = attr;
- };
-
- /**
- * Sets the compression method of the entry.
- *
- * @param method
- */
- ZipArchiveEntry.prototype.setMethod = function(method) {
- if (method < 0) {
- throw new Error('invalid entry compression method');
- }
-
- this.method = method;
- };
-
- /**
- * Sets the name of the entry.
- *
- * @param name
- * @param prependSlash
- */
- ZipArchiveEntry.prototype.setName = function(name, prependSlash = false) {
- name = normalizePath(name, false)
- .replace(/^\w+:/, '')
- .replace(/^(\.\.\/|\/)+/, '');
-
- if (prependSlash) {
- name = `/${name}`;
- }
-
- if (Buffer.byteLength(name) !== name.length) {
- this.getGeneralPurposeBit().useUTF8ForNames(true);
- }
-
- this.name = name;
- };
-
- /**
- * Sets the platform on which the entry was made.
- *
- * @param platform
- */
- ZipArchiveEntry.prototype.setPlatform = function(platform) {
- this.platform = platform;
- };
-
- /**
- * Sets the size of the entry.
- *
- * @param size
- */
- ZipArchiveEntry.prototype.setSize = function(size) {
- if (size < 0) {
- throw new Error('invalid entry size');
- }
-
- this.size = size;
- };
-
- /**
- * Sets the time of the entry.
- *
- * @param time
- * @param forceLocalTime
- */
- ZipArchiveEntry.prototype.setTime = function(time, forceLocalTime) {
- if (!(time instanceof Date)) {
- throw new Error('invalid entry time');
- }
-
- this.time = zipUtil.dateToDos(time, forceLocalTime);
- };
-
- /**
- * Sets the UNIX file permissions for the entry.
- *
- * @param mode
- */
- ZipArchiveEntry.prototype.setUnixMode = function(mode) {
- mode |= this.isDirectory() ? constants.S_IFDIR : constants.S_IFREG;
-
- var extattr = 0;
- extattr |= (mode << constants.SHORT_SHIFT) | (this.isDirectory() ? constants.S_DOS_D : constants.S_DOS_A);
-
- this.setExternalAttributes(extattr);
- this.mode = mode & constants.MODE_MASK;
- this.platform = constants.PLATFORM_UNIX;
- };
-
- /**
- * Sets the version of ZIP needed to extract this entry.
- *
- * @param minver
- */
- ZipArchiveEntry.prototype.setVersionNeededToExtract = function(minver) {
- this.minver = minver;
- };
-
- /**
- * Returns true if this entry represents a directory.
- *
- * @returns {boolean}
- */
- ZipArchiveEntry.prototype.isDirectory = function() {
- return this.getName().slice(-1) === '/';
- };
-
- /**
- * Returns true if this entry represents a unix symlink,
- * in which case the entry's content contains the target path
- * for the symlink.
- *
- * @returns {boolean}
- */
- ZipArchiveEntry.prototype.isUnixSymlink = function() {
- return (this.getUnixMode() & UnixStat.FILE_TYPE_FLAG) === UnixStat.LINK_FLAG;
- };
-
- /**
- * Returns true if this entry is using the ZIP64 extension of ZIP.
- *
- * @returns {boolean}
- */
- ZipArchiveEntry.prototype.isZip64 = function() {
- return this.csize > constants.ZIP64_MAGIC || this.size > constants.ZIP64_MAGIC;
- };
|