New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

pizzip

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pizzip - npm Package Compare versions

Comparing version 3.1.2 to 3.1.3

4

CHANGES.md

@@ -7,2 +7,6 @@ ---

# v3.1.3
Update typescript typings to work correctly.
# v3.1.2

@@ -9,0 +13,0 @@

595

es6/index.d.ts

@@ -1,330 +0,385 @@

// Type definitions for JSZip 3.1
// Project: http://stuk.github.com/jszip/, https://github.com/stuk/jszip
// Definitions by: mzeiher <https://github.com/mzeiher>, forabi <https://github.com/forabi>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
// Type definitions for pizzip 3.0
// Copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/pizzip/pizzip-tests.ts
/// <reference types="node" />
interface JSZipSupport {
arraybuffer: boolean;
uint8array: boolean;
blob: boolean;
nodebuffer: boolean;
}
export as namespace PizZip;
type Compression = 'STORE' | 'DEFLATE';
export = PizZip;
/**
* Depends on the compression type. With `STORE` (no compression), these options are ignored. With
* `DEFLATE`, you can give the compression level between 1 (best speed) and 9 (best compression).
*/
interface CompressionOptions {
level: number;
}
declare class PizZip {
/**
* If the browser supports them, PizZip can take advantage of some "new" features : ArrayBuffer, Blob, Uint8Array.
* To know if PizZip can use them, you can check the PizZip.support object.
*/
static support: {
/**
* true if PizZip can read and generate ArrayBuffer, false otherwise.
*/
arraybuffer: boolean;
/**
* true if PizZip can read and generate Uint8Array, false otherwise.
*/
uint8array: boolean;
/**
* true if PizZip can read and generate Blob, false otherwise.
*/
blob: boolean;
/**
* true if PizZip can read and generate nodejs Buffer, false otherwise.
*/
nodebuffer: boolean;
};
interface InputByType {
base64: string;
string: string;
text: string;
binarystring: string;
array: number[];
uint8array: Uint8Array;
arraybuffer: ArrayBuffer;
blob: Blob;
stream: NodeJS.ReadableStream;
}
/**
* the ZipObjects inside the zip with the name as key.
*/
files: { [key: string]: PizZip.ZipObject };
interface OutputByType {
base64: string;
string: string;
text: string;
binarystring: string;
array: number[];
uint8array: Uint8Array;
arraybuffer: ArrayBuffer;
blob: Blob;
nodebuffer: Buffer;
}
/**
* the comment of the zip file.
*/
comment: string;
// This private `_data` property on a JSZipObject uses this interface.
// If/when it is made public this should be uncommented.
// interface CompressedObject {
// compressedSize: number;
// uncompressedSize: number;
// crc32: number;
// compression: object;
// compressedContent: string|ArrayBuffer|Uint8Array|Buffer;
// }
constructor();
type InputFileFormat = InputByType[keyof InputByType] | Promise<InputByType[keyof InputByType]>;
/**
* Specifying data & options is a shortcut for new PizZip().load(data, options);
*
* @param data the zip file
* @param options the options to load the zip file
*/
// tslint:disable-next-line unified-signatures new (undefined, {options}) is not an acceptable input
constructor(data: PizZip.LoadData, options?: PizZip.LoadOptions);
declare namespace JSZip {
type InputType = keyof InputByType;
/**
* Read an existing zip and merge the data in the current PizZip object at the current folder level.
* This technique has some limitations, see https://github.com/open-xml-templating/pizzip/blob/master/documentation/limitations.md
* You shouldn't update the data given to this method : it is kept as it so any update will impact the stored data.
* Throws an exception if the loaded data is not valid zip data or if it uses features (multi volume, password protected, etc).
* @param data the zip file
* @param options the options to load the zip file
*/
load(data: PizZip.LoadData, options?: PizZip.LoadOptions): this;
type OutputType = keyof OutputByType;
/**
* Add (or update) a file to the zip file.
* You shouldn't update the data given to this method : it is kept as it so any update will impact the stored data.
* Throws an exception if the data is not in a supported format.
* @param name the name of the file. You can specify folders in the name : the folder separator is a forward slash ("/").
* @param data the content of the file.
* @param options the options.
*/
file(name: string, data: PizZip.Data, options?: PizZip.FileOptions): this;
interface JSZipMetadata {
percent: number;
currentFile: string | null;
}
/**
* Get a file with the specified name. You can specify folders in the name : the folder separator is a forward slash ("/").
* @param name the name of the file.
*/
file(name: string): PizZip.ZipObject | null;
type OnUpdateCallback = (metadata: JSZipMetadata) => void;
/**
* Search a file in the current folder and subfolders with a regular expression. The regex is tested against the relative filename.
* @param regex the regex to use.
*/
file(regex: RegExp): PizZip.ZipObject[];
interface JSZipObject {
/**
* Filter nested files/folders with the specified function. The predicate must return true if the file should be included, false otherwise.
* @param predicate the predicate to use.
*/
filter(
predicate: (
/**
* the filename and its path, reliatively to the current folder.
*/
relativePath: string,
/**
* the file being tested
*/
file: PizZip.ZipObject,
) => boolean,
): PizZip.ZipObject[];
/**
* Create a directory if it doesn't exist, return a new PizZip object with the new folder as root.
* @param name the name of the directory.
*/
folder(name: string): this;
/**
* Search a subdirectory in the current directory with a regular expression. The regex is tested against the relative path.
* @param regex the regex to use.
*/
folder(regex: RegExp): PizZip.ZipObject[];
/**
* Generates the complete zip file.
* Throws an exception if the asked type is not available in the browser,
* see https://github.com/open-xml-templating/pizzip/blob/master/documentation/api_pizzip/support.md
* @param options the options to generate the zip file
*/
generate(options?: PizZip.GenerateOptions & { type?: 'string' | 'base64' | undefined }): string;
generate(options: PizZip.GenerateOptions & { type: 'blob' }): Blob;
generate(options: PizZip.GenerateOptions & { type: 'nodebuffer' }): Buffer;
generate(options: PizZip.GenerateOptions & { type: 'arraybuffer' }): ArrayBuffer;
generate(options: PizZip.GenerateOptions & { type: 'uint8array' }): Uint8Array;
/**
* Delete a file or folder (recursively).
* @param name the name of the file/folder to delete.
*/
remove(name: string): this;
}
declare namespace PizZip {
type Compression = 'STORE' | 'DEFLATE';
type Data = string | ArrayBuffer | Uint8Array | Buffer;
type LoadData = Data | number[];
interface ZipObject {
/**
* the absolute path of the file
*/
name: string;
/**
* Present for files loadded with `loadAsync`. May contain ".." path components that could
* result in a zip-slip attack. See https://snyk.io/research/zip-slip-vulnerability
* true if this is a directory
*/
unsafeOriginalName?: string;
dir: boolean;
/**
* the last modification date
*/
date: Date;
/**
* the comment for this file
*/
comment: string;
/** The UNIX permissions of the file, if any. */
unixPermissions: number | string | null;
/** The UNIX permissions of the file, if any. */
dosPermissions: number | null;
options: JSZipObjectOptions;
/**
* Prepare the content in the asked type.
* @param type the type of the result.
* @param onUpdate a function to call on each internal update.
* @return Promise the promise of the result.
* The UNIX permissions of the file, if any. Also accepts a string representing the octal value : "644", "755", etc. On nodejs you can use the mode attribute of nodejs' fs.Stats.
*/
async<T extends OutputType>(type: T, onUpdate?: OnUpdateCallback): Promise<OutputByType[T]>;
nodeStream(type?: 'nodebuffer', onUpdate?: OnUpdateCallback): NodeJS.ReadableStream;
}
unixPermissions: number | string;
/**
* The DOS permissions of the file, if any.
*/
dosPermissions: number;
/**
* the options of the file.
*/
options: {
/**
* @deprecated
*/
base64: boolean;
/**
* @deprecated
*/
binary: boolean;
/**
* @deprecated use File.dir
*/
dir: boolean;
/**
* @deprecated use File.date
*/
date: Date;
compression: Compression;
};
interface JSZipFileOptions {
/** Set to `true` if the data is `base64` encoded. For example image data from a `<canvas>` element. Plain text and HTML do not need this option. */
base64?: boolean;
/**
* Set to `true` if the data should be treated as raw content, `false` if this is a text. If `base64` is used,
* this defaults to `true`, if the data is not a `string`, this will be set to `true`.
* the content as an unicode string.
*/
binary?: boolean;
asText(): string;
/**
* The last modification date, defaults to the current date.
* the content as binary string.
*/
date?: Date;
asBinary(): string;
/**
* Sets per file compression. The `compressionOptions` parameter depends on the compression type.
* need a compatible browser. https://github.com/open-xml-templating/pizzip/blob/master/documentation/api_pizzip/support.md
*/
compression?: Compression;
asArrayBuffer(): ArrayBuffer;
/**
* Sets per file compression level for `DEFLATE` compression.
* need a compatible browser. https://github.com/open-xml-templating/pizzip/blob/master/documentation/api_pizzip/support.md
*/
compressionOptions?: null | CompressionOptions;
comment?: string;
/** Set to `true` if (and only if) the input is a "binary string" and has already been prepared with a `0xFF` mask. */
optimizedBinaryString?: boolean;
/** Set to `true` if folders in the file path should be automatically created, otherwise there will only be virtual folders that represent the path to the file. */
createFolders?: boolean;
/** Set to `true` if this is a directory and content should be ignored. */
dir?: boolean;
asUint8Array(): Uint8Array;
/** 6 bits number. The DOS permissions of the file, if any. */
dosPermissions?: number | null;
/**
* 16 bits number. The UNIX permissions of the file, if any.
* Also accepts a `string` representing the octal value: `"644"`, `"755"`, etc.
* need nodejs. https://github.com/open-xml-templating/pizzip/blob/master/documentation/api_pizzip/support.md
*/
unixPermissions?: number | string | null;
asNodeBuffer(): Buffer;
}
interface JSZipObjectOptions {
compression: Compression;
interface LoadOptions {
/**
* set to true if the data is base64 encoded, false for binary.
*
* @default false
*/
base64?: boolean | undefined;
/**
* set to true if the read data should be checked against its CRC32.
*
* @default false
*/
checkCRC32?: boolean | undefined;
/**
* set to true if (and only if) the input is a string and has already been prepared with a 0xFF mask.
*
* @default false
*/
optimizedBinaryString?: boolean | undefined;
/**
* set to true to create folders in the file path automatically. Leaving it false will result in only virtual folders
* (i.e. folders that merely represent part of the file path) being created.
*
* @default false
*/
createFolders?: boolean | undefined;
/**
* the function to decode the file name / comment. Decodes from UTF-8 by default.
* A zip file has a flag to say if the filename and comment are encoded with UTF-8.
* If it's not set, PizZip has no way to know the encoding used. It usually is the default encoding of the operating system.
* The function takes the bytes array (Uint8Array or Array) and returns the decoded string.
*/
decodeFileName?(bytes: Uint8Array | number[]): string;
}
interface JSZipGeneratorOptions<T extends OutputType = OutputType> {
interface FileOptions {
/**
* Sets compression option for all entries that have not specified their own `compression` option
* set to `true` if the data is base64 encoded. For example image data from a `<canvas>` element. Plain text and HTML do not need this option.
* @default false
*/
compression?: Compression;
base64?: boolean | undefined;
/**
* Sets compression level for `DEFLATE` compression.
* set to `true` if the data should be treated as raw content, `false` if this is a text. If base64 is used, this defaults to `true`,
* if the data is not a string, this will be set to `true`.
* @default false
*/
compressionOptions?: null | CompressionOptions;
type?: T;
comment?: string;
binary?: boolean | undefined;
/**
* mime-type for the generated file.
* Useful when you need to generate a file with a different extension, ie: “.ods”.
* @default 'application/zip'
* the last modification date. defaults to the current date
*/
mimeType?: string;
encodeFileName?(filename: string): string;
/** Stream the files and create file descriptors */
streamFiles?: boolean;
/** DOS (default) or UNIX */
platform?: 'DOS' | 'UNIX';
date?: Date | undefined;
/**
* If set, specifies compression method to use for this specific file. If not, the default file compression will be used (no compression)
* @default "STORE"
*/
compression?: Compression | undefined;
/**
* With `STORE` (no compression), this parameter is ignored.
* With `DEFLATE`, you can give the compression level with `compressionOptions : {level:6}` (or any level between 1 (best speed) and 9 (best compression)).
*/
compressionOptions?: {
level: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
} | null | undefined;
/**
* The comment for this file.
* the zip format has no flag or field to give the encoding of this field and PizZip will use UTF-8.
* With non ASCII characters you might get encoding issues if the file archiver doesn't use UTF-8 to decode the comment.
* If not set, PizZip will use the field comment on its options.
* @default null
*/
comment?: string | null | undefined;
/**
* Set to true if (and only if) the input is a "binary string" and has already been prepared with a 0xFF mask.
* @default false
*/
optimizedBinaryString?: boolean | undefined;
/**
* Set to true if folders in the file path should be automatically created,
* otherwise there will only be virtual folders that represent the path to the file.
* @default false
*/
createFolders?: boolean | undefined;
/**
* The UNIX permissions of the file, if any. Also accepts a string representing the octal value : "644", "755", etc.
* On nodejs you can use the `mode` attribute of nodejs' fs.Stats.
* @default null
*/
unixPermissions?: number | string | null | undefined;
/**
* The DOS permissions of the file, if any.
* @default null
*/
dosPermissions?: number | null | undefined;
/**
* Set to true if this is a directory and content should be ignored.
* If true or if a permission says it's a folder, this entry be flagged as a folder and the content will be ignored.
* @default false
*/
dir?: boolean | undefined;
}
interface JSZipLoadOptions {
base64?: boolean;
checkCRC32?: boolean;
optimizedBinaryString?: boolean;
createFolders?: boolean;
decodeFileName?: (bytes: string[] | Uint8Array | Buffer) => string;
}
type DataEventCallback<T> = (dataChunk: T, metadata: JSZipMetadata) => void
type EndEventCallback = () => void
type ErrorEventCallback = (error: Error) => void
interface JSZipStreamHelper<T> {
interface GenerateOptions {
/**
* Register a listener on an event
* @deprecated use `type` instead. If `type` is not used, set to `false` to get the result as a raw byte string, `true` to encode it as base64.
* @default false
*/
on(event: 'data', callback: DataEventCallback<T>): this;
on(event: 'end', callback: EndEventCallback): this;
on(event: 'error', callback: ErrorEventCallback): this;
base64?: boolean | undefined;
/**
* Read the whole stream and call a callback with the complete content
* the default file compression method to use. Available methods are `STORE` and `DEFLATE`. You can also provide your own compression method.
* @default "STORE"
*/
compression?: Compression | undefined;
/**
* the options to use when compressing the file. With `STORE` (no compression), this parameter is ignored.
* With `DEFLATE`, you can give the compression level with `compressionOptions : {level:6}`
* (or any level between 1 (best speed) and 9 (best compression)).
*
* @param updateCallback The function called every time the stream updates
* @return A Promise of the full content
* Note : if the entry is already compressed (coming from a compressed zip file),
* calling `generate()` with a different compression level won't update the entry.
* The reason is simple : PizZip doesn't know how compressed the content was and how to match the compression level with the implementation we use.
*/
accumulate(updateCallback?: (metadata: JSZipMetadata) => void): Promise<T>;
compressionOptions?: {
level: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
} | null | undefined;
/**
* Resume the stream if the stream is paused. Once resumed, the stream starts sending data events again
* The type of zip to return. Note : when using type = "uint8array", "arraybuffer" or "blob",
* be sure to check if the browser supports it (you can use PizZip.support)
*
* @return The current StreamHelper object, for chaining
* `base64` : the result will be a string, the binary in a base64 form.
*
* `string` : the result will be a string in "binary" form, using 1 byte per char (2 bytes).
*
* `uint8array` : the result will be a Uint8Array containing the zip. This requires a compatible browser.
*
* `arraybuffer` : the result will be a ArrayBuffer containing the zip. This requires a compatible browser.
*
* `blob` : the result will be a Blob containing the zip. This requires a compatible browser.
*
* `nodebuffer` : the result will be a nodejs Buffer containing the zip. This requires nodejs.
*
* @default "base64"
*/
resume(): this;
type?: 'base64' | 'string' | 'uint8array' | 'arraybuffer' | 'blob' | 'nodebuffer' | undefined;
/**
* Pause the stream if the stream is running. Once paused, the stream stops sending data events
* The comment to use for the zip file.
*/
comment?: string | undefined;
/**
* mime-type for the generated file. Useful when you need to generate a file with a different extension, ie: ".ods".
*
* @return The current StreamHelper object, for chaining
* @default "application/zip"
*/
pause(): this;
mimeType?: string | undefined;
/**
* The platform to use when generating the zip file. When using `DOS`, the attribute `dosPermissions` of each file is used.
* When using `UNIX`, the attribute `unixPermissions` of each file is used.
* If you set the platform value on nodejs, be sure to use `process.platform`.
* `fs.stats` returns a non executable mode for folders on windows,
* if you force the platform to `UNIX` the generated zip file will have a strange behavior on UNIX platforms.
* @default "DOS"
*/
platform?: 'DOS' | 'UNIX' | NodeJS.Platform | undefined;
/**
* the function to encode the file name / comment.
* By default, PizZip uses UTF-8 to encode the file names / comments. You can use this method to force an other encoding.
* Note : the encoding used is not stored in a zip file, not using UTF-8 may lead to encoding issues.
* The function takes a string and returns a bytes array (Uint8Array or Array).
*/
encodeFileName?(name: string): Buffer;
}
}
interface JSZip {
files: {[key: string]: JSZip.JSZipObject};
/**
* Get a file from the archive
*
* @param Path relative path to file
* @return File matching path, null if no file found
*/
file(path: string): JSZip.JSZipObject | null;
/**
* Get files matching a RegExp from archive
*
* @param path RegExp to match
* @return Return all matching files or an empty array
*/
file(path: RegExp): JSZip.JSZipObject[];
/**
* Add a file to the archive
*
* @param path Relative path to file
* @param data Content of the file
* @param options Optional information about the file
* @return JSZip object
*/
file<T extends JSZip.InputType>(path: string, data: InputByType[T] | Promise<InputByType[T]>, options?: JSZip.JSZipFileOptions): this;
file<T extends JSZip.InputType>(path: string, data: null, options?: JSZip.JSZipFileOptions & { dir: true }): this;
/**
* Returns an new JSZip instance with the given folder as root
*
* @param name Name of the folder
* @return New JSZip object with the given folder as root or null
*/
folder(name: string): JSZip | null;
/**
* Returns new JSZip instances with the matching folders as root
*
* @param name RegExp to match
* @return New array of JSZipFile objects which match the RegExp
*/
folder(name: RegExp): JSZip.JSZipObject[];
/**
* Call a callback function for each entry at this folder level.
*
* @param callback function
*/
forEach(callback: (relativePath: string, file: JSZip.JSZipObject) => void): void;
/**
* Get all files which match the given filter function
*
* @param predicate Filter function
* @return Array of matched elements
*/
filter(predicate: (relativePath: string, file: JSZip.JSZipObject) => boolean): JSZip.JSZipObject[];
/**
* Removes the file or folder from the archive
*
* @param path Relative path of file or folder
* @return Returns the JSZip instance
*/
remove(path: string): JSZip;
/**
* Generates a new archive asynchronously
*
* @param options Optional options for the generator
* @param onUpdate The optional function called on each internal update with the metadata.
* @return The serialized archive
*/
generate<T extends JSZip.OutputType>(options?: JSZip.JSZipGeneratorOptions<T>, onUpdate?: JSZip.OnUpdateCallback): OutputByType[T];
/**
* Generates a new archive
*
* @param options Optional options for the generator
* @param onUpdate The optional function called on each internal update with the metadata.
* @return A Node.js `ReadableStream`
*/
generateNodeStream(options?: JSZip.JSZipGeneratorOptions<'nodebuffer'>, onUpdate?: JSZip.OnUpdateCallback): NodeJS.ReadableStream;
/**
* Generates the complete zip file with the internal stream implementation
*
* @param options Optional options for the generator
* @return a StreamHelper
*/
generateInternalStream<T extends JSZip.OutputType>(options?: JSZip.JSZipGeneratorOptions<T>): JSZip.JSZipStreamHelper<OutputByType[T]>;
/**
* Deserialize zip file asynchronously
*
* @param data Serialized zip file
* @param options Options for deserializing
* @return Returns promise
*/
load(data: InputFileFormat, options?: JSZip.JSZipLoadOptions): JSZip;
/**
* Create JSZip instance
*/
new(): this;
(): JSZip;
prototype: JSZip;
support: JSZipSupport;
external: {
Promise: PromiseConstructorLike;
};
version: string;
}
declare var JSZip: JSZip;
export = JSZip;
import PizZip from "./index.js";
import { expectType, expectError } from "tsd";
import * as fs from "fs";
const zip = new PizZip();
const zip1 = new PizZip();
zip.file(
"smile.gif",
"R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs=",
{ base64: true }
zip1.file(
"smile.gif",
"R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs=",
{ base64: true }
);
expectError(zip.foo(
"xxx"
));
expectError(zip1.foo("xxx"));

@@ -22,9 +21,80 @@ var zip3 = new PizZip();

const output = zip.generate({
type: "blob",
mimeType:
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
compression: "DEFLATE",
const output = zip1.generate({
type: "blob",
mimeType:
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
compression: "DEFLATE",
});
expectError(zip.generate({optionXYZ: true}));
expectError(zip1.generate({ optionXYZ: true }));
const zip = new PizZip();
if (PizZip.support.blob) {
/* amazing */
}
new PizZip("data", {
base64: true,
checkCRC32: true,
createFolders: true,
optimizedBinaryString: true,
decodeFileName: (a) => "",
});
zip
.file("Hello.txt", "Hello World\n")
.file(
"smile.gif",
"R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs=",
{ base64: true }
)
.file("smile.gif", fs.readFileSync("smile.gif"))
.file("Xmas.txt", "Ho ho ho !", {
date: new Date("December 25, 2007 00:00:01"),
})
.file("folder/file.txt", "file in folder", { createFolders: true })
.file("script.sh", "echo 'hello world'", { unixPermissions: "755" })
.folder("css")
.remove("css");
zip.load("content", { decodeFileName: (bytes) => "encoding" });
zip.file(/file/).map((z) => z);
zip.folder(/^vid/).map((z) => z);
zip.filter((relativePath, file) => true).map((z) => z);
zip.folder("subfolder").load("data");
const file = zip.file("");
if (file) {
const nameTest: string = file.name;
const textTest: string = file.asText();
const binaryTest: string = file.asBinary();
const arrBufTest: ArrayBuffer = file.asArrayBuffer();
const uint8Test: Uint8Array = file.asUint8Array();
const bufTest: Buffer = file.asNodeBuffer();
}
const noOptionsTest: string = zip.generate();
const noTypeTest: string = zip.generate({ base64: true });
const b64Test: string = zip.generate({
type: "base64",
compression: "DEFLATE",
});
const stringTest: string = zip.generate({
type: "string",
encodeFileName: (s) => new Buffer(s),
});
const arrBufTest: ArrayBuffer = zip.generate({
type: "arraybuffer",
mimeType: "",
});
const blobTest: Blob = zip.generate({
type: "blob",
compressionOptions: { level: 1 },
});
const bufTest: Buffer = zip.generate({ type: "nodebuffer", platform: "DOS" });
const unit8Test: Uint8Array = zip.generate({
type: "uint8array",
base64: true,
});

@@ -1,330 +0,385 @@

// Type definitions for JSZip 3.1
// Project: http://stuk.github.com/jszip/, https://github.com/stuk/jszip
// Definitions by: mzeiher <https://github.com/mzeiher>, forabi <https://github.com/forabi>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
// Type definitions for pizzip 3.0
// Copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/pizzip/pizzip-tests.ts
/// <reference types="node" />
interface JSZipSupport {
arraybuffer: boolean;
uint8array: boolean;
blob: boolean;
nodebuffer: boolean;
}
export as namespace PizZip;
type Compression = 'STORE' | 'DEFLATE';
export = PizZip;
/**
* Depends on the compression type. With `STORE` (no compression), these options are ignored. With
* `DEFLATE`, you can give the compression level between 1 (best speed) and 9 (best compression).
*/
interface CompressionOptions {
level: number;
}
declare class PizZip {
/**
* If the browser supports them, PizZip can take advantage of some "new" features : ArrayBuffer, Blob, Uint8Array.
* To know if PizZip can use them, you can check the PizZip.support object.
*/
static support: {
/**
* true if PizZip can read and generate ArrayBuffer, false otherwise.
*/
arraybuffer: boolean;
/**
* true if PizZip can read and generate Uint8Array, false otherwise.
*/
uint8array: boolean;
/**
* true if PizZip can read and generate Blob, false otherwise.
*/
blob: boolean;
/**
* true if PizZip can read and generate nodejs Buffer, false otherwise.
*/
nodebuffer: boolean;
};
interface InputByType {
base64: string;
string: string;
text: string;
binarystring: string;
array: number[];
uint8array: Uint8Array;
arraybuffer: ArrayBuffer;
blob: Blob;
stream: NodeJS.ReadableStream;
}
/**
* the ZipObjects inside the zip with the name as key.
*/
files: { [key: string]: PizZip.ZipObject };
interface OutputByType {
base64: string;
string: string;
text: string;
binarystring: string;
array: number[];
uint8array: Uint8Array;
arraybuffer: ArrayBuffer;
blob: Blob;
nodebuffer: Buffer;
}
/**
* the comment of the zip file.
*/
comment: string;
// This private `_data` property on a JSZipObject uses this interface.
// If/when it is made public this should be uncommented.
// interface CompressedObject {
// compressedSize: number;
// uncompressedSize: number;
// crc32: number;
// compression: object;
// compressedContent: string|ArrayBuffer|Uint8Array|Buffer;
// }
constructor();
type InputFileFormat = InputByType[keyof InputByType] | Promise<InputByType[keyof InputByType]>;
/**
* Specifying data & options is a shortcut for new PizZip().load(data, options);
*
* @param data the zip file
* @param options the options to load the zip file
*/
// tslint:disable-next-line unified-signatures new (undefined, {options}) is not an acceptable input
constructor(data: PizZip.LoadData, options?: PizZip.LoadOptions);
declare namespace JSZip {
type InputType = keyof InputByType;
/**
* Read an existing zip and merge the data in the current PizZip object at the current folder level.
* This technique has some limitations, see https://github.com/open-xml-templating/pizzip/blob/master/documentation/limitations.md
* You shouldn't update the data given to this method : it is kept as it so any update will impact the stored data.
* Throws an exception if the loaded data is not valid zip data or if it uses features (multi volume, password protected, etc).
* @param data the zip file
* @param options the options to load the zip file
*/
load(data: PizZip.LoadData, options?: PizZip.LoadOptions): this;
type OutputType = keyof OutputByType;
/**
* Add (or update) a file to the zip file.
* You shouldn't update the data given to this method : it is kept as it so any update will impact the stored data.
* Throws an exception if the data is not in a supported format.
* @param name the name of the file. You can specify folders in the name : the folder separator is a forward slash ("/").
* @param data the content of the file.
* @param options the options.
*/
file(name: string, data: PizZip.Data, options?: PizZip.FileOptions): this;
interface JSZipMetadata {
percent: number;
currentFile: string | null;
}
/**
* Get a file with the specified name. You can specify folders in the name : the folder separator is a forward slash ("/").
* @param name the name of the file.
*/
file(name: string): PizZip.ZipObject | null;
type OnUpdateCallback = (metadata: JSZipMetadata) => void;
/**
* Search a file in the current folder and subfolders with a regular expression. The regex is tested against the relative filename.
* @param regex the regex to use.
*/
file(regex: RegExp): PizZip.ZipObject[];
interface JSZipObject {
/**
* Filter nested files/folders with the specified function. The predicate must return true if the file should be included, false otherwise.
* @param predicate the predicate to use.
*/
filter(
predicate: (
/**
* the filename and its path, reliatively to the current folder.
*/
relativePath: string,
/**
* the file being tested
*/
file: PizZip.ZipObject,
) => boolean,
): PizZip.ZipObject[];
/**
* Create a directory if it doesn't exist, return a new PizZip object with the new folder as root.
* @param name the name of the directory.
*/
folder(name: string): this;
/**
* Search a subdirectory in the current directory with a regular expression. The regex is tested against the relative path.
* @param regex the regex to use.
*/
folder(regex: RegExp): PizZip.ZipObject[];
/**
* Generates the complete zip file.
* Throws an exception if the asked type is not available in the browser,
* see https://github.com/open-xml-templating/pizzip/blob/master/documentation/api_pizzip/support.md
* @param options the options to generate the zip file
*/
generate(options?: PizZip.GenerateOptions & { type?: 'string' | 'base64' | undefined }): string;
generate(options: PizZip.GenerateOptions & { type: 'blob' }): Blob;
generate(options: PizZip.GenerateOptions & { type: 'nodebuffer' }): Buffer;
generate(options: PizZip.GenerateOptions & { type: 'arraybuffer' }): ArrayBuffer;
generate(options: PizZip.GenerateOptions & { type: 'uint8array' }): Uint8Array;
/**
* Delete a file or folder (recursively).
* @param name the name of the file/folder to delete.
*/
remove(name: string): this;
}
declare namespace PizZip {
type Compression = 'STORE' | 'DEFLATE';
type Data = string | ArrayBuffer | Uint8Array | Buffer;
type LoadData = Data | number[];
interface ZipObject {
/**
* the absolute path of the file
*/
name: string;
/**
* Present for files loadded with `loadAsync`. May contain ".." path components that could
* result in a zip-slip attack. See https://snyk.io/research/zip-slip-vulnerability
* true if this is a directory
*/
unsafeOriginalName?: string;
dir: boolean;
/**
* the last modification date
*/
date: Date;
/**
* the comment for this file
*/
comment: string;
/** The UNIX permissions of the file, if any. */
unixPermissions: number | string | null;
/** The UNIX permissions of the file, if any. */
dosPermissions: number | null;
options: JSZipObjectOptions;
/**
* Prepare the content in the asked type.
* @param type the type of the result.
* @param onUpdate a function to call on each internal update.
* @return Promise the promise of the result.
* The UNIX permissions of the file, if any. Also accepts a string representing the octal value : "644", "755", etc. On nodejs you can use the mode attribute of nodejs' fs.Stats.
*/
async<T extends OutputType>(type: T, onUpdate?: OnUpdateCallback): Promise<OutputByType[T]>;
nodeStream(type?: 'nodebuffer', onUpdate?: OnUpdateCallback): NodeJS.ReadableStream;
}
unixPermissions: number | string;
/**
* The DOS permissions of the file, if any.
*/
dosPermissions: number;
/**
* the options of the file.
*/
options: {
/**
* @deprecated
*/
base64: boolean;
/**
* @deprecated
*/
binary: boolean;
/**
* @deprecated use File.dir
*/
dir: boolean;
/**
* @deprecated use File.date
*/
date: Date;
compression: Compression;
};
interface JSZipFileOptions {
/** Set to `true` if the data is `base64` encoded. For example image data from a `<canvas>` element. Plain text and HTML do not need this option. */
base64?: boolean;
/**
* Set to `true` if the data should be treated as raw content, `false` if this is a text. If `base64` is used,
* this defaults to `true`, if the data is not a `string`, this will be set to `true`.
* the content as an unicode string.
*/
binary?: boolean;
asText(): string;
/**
* The last modification date, defaults to the current date.
* the content as binary string.
*/
date?: Date;
asBinary(): string;
/**
* Sets per file compression. The `compressionOptions` parameter depends on the compression type.
* need a compatible browser. https://github.com/open-xml-templating/pizzip/blob/master/documentation/api_pizzip/support.md
*/
compression?: Compression;
asArrayBuffer(): ArrayBuffer;
/**
* Sets per file compression level for `DEFLATE` compression.
* need a compatible browser. https://github.com/open-xml-templating/pizzip/blob/master/documentation/api_pizzip/support.md
*/
compressionOptions?: null | CompressionOptions;
comment?: string;
/** Set to `true` if (and only if) the input is a "binary string" and has already been prepared with a `0xFF` mask. */
optimizedBinaryString?: boolean;
/** Set to `true` if folders in the file path should be automatically created, otherwise there will only be virtual folders that represent the path to the file. */
createFolders?: boolean;
/** Set to `true` if this is a directory and content should be ignored. */
dir?: boolean;
asUint8Array(): Uint8Array;
/** 6 bits number. The DOS permissions of the file, if any. */
dosPermissions?: number | null;
/**
* 16 bits number. The UNIX permissions of the file, if any.
* Also accepts a `string` representing the octal value: `"644"`, `"755"`, etc.
* need nodejs. https://github.com/open-xml-templating/pizzip/blob/master/documentation/api_pizzip/support.md
*/
unixPermissions?: number | string | null;
asNodeBuffer(): Buffer;
}
interface JSZipObjectOptions {
compression: Compression;
interface LoadOptions {
/**
* set to true if the data is base64 encoded, false for binary.
*
* @default false
*/
base64?: boolean | undefined;
/**
* set to true if the read data should be checked against its CRC32.
*
* @default false
*/
checkCRC32?: boolean | undefined;
/**
* set to true if (and only if) the input is a string and has already been prepared with a 0xFF mask.
*
* @default false
*/
optimizedBinaryString?: boolean | undefined;
/**
* set to true to create folders in the file path automatically. Leaving it false will result in only virtual folders
* (i.e. folders that merely represent part of the file path) being created.
*
* @default false
*/
createFolders?: boolean | undefined;
/**
* the function to decode the file name / comment. Decodes from UTF-8 by default.
* A zip file has a flag to say if the filename and comment are encoded with UTF-8.
* If it's not set, PizZip has no way to know the encoding used. It usually is the default encoding of the operating system.
* The function takes the bytes array (Uint8Array or Array) and returns the decoded string.
*/
decodeFileName?(bytes: Uint8Array | number[]): string;
}
interface JSZipGeneratorOptions<T extends OutputType = OutputType> {
interface FileOptions {
/**
* Sets compression option for all entries that have not specified their own `compression` option
* set to `true` if the data is base64 encoded. For example image data from a `<canvas>` element. Plain text and HTML do not need this option.
* @default false
*/
compression?: Compression;
base64?: boolean | undefined;
/**
* Sets compression level for `DEFLATE` compression.
* set to `true` if the data should be treated as raw content, `false` if this is a text. If base64 is used, this defaults to `true`,
* if the data is not a string, this will be set to `true`.
* @default false
*/
compressionOptions?: null | CompressionOptions;
type?: T;
comment?: string;
binary?: boolean | undefined;
/**
* mime-type for the generated file.
* Useful when you need to generate a file with a different extension, ie: “.ods”.
* @default 'application/zip'
* the last modification date. defaults to the current date
*/
mimeType?: string;
encodeFileName?(filename: string): string;
/** Stream the files and create file descriptors */
streamFiles?: boolean;
/** DOS (default) or UNIX */
platform?: 'DOS' | 'UNIX';
date?: Date | undefined;
/**
* If set, specifies compression method to use for this specific file. If not, the default file compression will be used (no compression)
* @default "STORE"
*/
compression?: Compression | undefined;
/**
* With `STORE` (no compression), this parameter is ignored.
* With `DEFLATE`, you can give the compression level with `compressionOptions : {level:6}` (or any level between 1 (best speed) and 9 (best compression)).
*/
compressionOptions?: {
level: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
} | null | undefined;
/**
* The comment for this file.
* the zip format has no flag or field to give the encoding of this field and PizZip will use UTF-8.
* With non ASCII characters you might get encoding issues if the file archiver doesn't use UTF-8 to decode the comment.
* If not set, PizZip will use the field comment on its options.
* @default null
*/
comment?: string | null | undefined;
/**
* Set to true if (and only if) the input is a "binary string" and has already been prepared with a 0xFF mask.
* @default false
*/
optimizedBinaryString?: boolean | undefined;
/**
* Set to true if folders in the file path should be automatically created,
* otherwise there will only be virtual folders that represent the path to the file.
* @default false
*/
createFolders?: boolean | undefined;
/**
* The UNIX permissions of the file, if any. Also accepts a string representing the octal value : "644", "755", etc.
* On nodejs you can use the `mode` attribute of nodejs' fs.Stats.
* @default null
*/
unixPermissions?: number | string | null | undefined;
/**
* The DOS permissions of the file, if any.
* @default null
*/
dosPermissions?: number | null | undefined;
/**
* Set to true if this is a directory and content should be ignored.
* If true or if a permission says it's a folder, this entry be flagged as a folder and the content will be ignored.
* @default false
*/
dir?: boolean | undefined;
}
interface JSZipLoadOptions {
base64?: boolean;
checkCRC32?: boolean;
optimizedBinaryString?: boolean;
createFolders?: boolean;
decodeFileName?: (bytes: string[] | Uint8Array | Buffer) => string;
}
type DataEventCallback<T> = (dataChunk: T, metadata: JSZipMetadata) => void
type EndEventCallback = () => void
type ErrorEventCallback = (error: Error) => void
interface JSZipStreamHelper<T> {
interface GenerateOptions {
/**
* Register a listener on an event
* @deprecated use `type` instead. If `type` is not used, set to `false` to get the result as a raw byte string, `true` to encode it as base64.
* @default false
*/
on(event: 'data', callback: DataEventCallback<T>): this;
on(event: 'end', callback: EndEventCallback): this;
on(event: 'error', callback: ErrorEventCallback): this;
base64?: boolean | undefined;
/**
* Read the whole stream and call a callback with the complete content
* the default file compression method to use. Available methods are `STORE` and `DEFLATE`. You can also provide your own compression method.
* @default "STORE"
*/
compression?: Compression | undefined;
/**
* the options to use when compressing the file. With `STORE` (no compression), this parameter is ignored.
* With `DEFLATE`, you can give the compression level with `compressionOptions : {level:6}`
* (or any level between 1 (best speed) and 9 (best compression)).
*
* @param updateCallback The function called every time the stream updates
* @return A Promise of the full content
* Note : if the entry is already compressed (coming from a compressed zip file),
* calling `generate()` with a different compression level won't update the entry.
* The reason is simple : PizZip doesn't know how compressed the content was and how to match the compression level with the implementation we use.
*/
accumulate(updateCallback?: (metadata: JSZipMetadata) => void): Promise<T>;
compressionOptions?: {
level: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
} | null | undefined;
/**
* Resume the stream if the stream is paused. Once resumed, the stream starts sending data events again
* The type of zip to return. Note : when using type = "uint8array", "arraybuffer" or "blob",
* be sure to check if the browser supports it (you can use PizZip.support)
*
* @return The current StreamHelper object, for chaining
* `base64` : the result will be a string, the binary in a base64 form.
*
* `string` : the result will be a string in "binary" form, using 1 byte per char (2 bytes).
*
* `uint8array` : the result will be a Uint8Array containing the zip. This requires a compatible browser.
*
* `arraybuffer` : the result will be a ArrayBuffer containing the zip. This requires a compatible browser.
*
* `blob` : the result will be a Blob containing the zip. This requires a compatible browser.
*
* `nodebuffer` : the result will be a nodejs Buffer containing the zip. This requires nodejs.
*
* @default "base64"
*/
resume(): this;
type?: 'base64' | 'string' | 'uint8array' | 'arraybuffer' | 'blob' | 'nodebuffer' | undefined;
/**
* Pause the stream if the stream is running. Once paused, the stream stops sending data events
* The comment to use for the zip file.
*/
comment?: string | undefined;
/**
* mime-type for the generated file. Useful when you need to generate a file with a different extension, ie: ".ods".
*
* @return The current StreamHelper object, for chaining
* @default "application/zip"
*/
pause(): this;
mimeType?: string | undefined;
/**
* The platform to use when generating the zip file. When using `DOS`, the attribute `dosPermissions` of each file is used.
* When using `UNIX`, the attribute `unixPermissions` of each file is used.
* If you set the platform value on nodejs, be sure to use `process.platform`.
* `fs.stats` returns a non executable mode for folders on windows,
* if you force the platform to `UNIX` the generated zip file will have a strange behavior on UNIX platforms.
* @default "DOS"
*/
platform?: 'DOS' | 'UNIX' | NodeJS.Platform | undefined;
/**
* the function to encode the file name / comment.
* By default, PizZip uses UTF-8 to encode the file names / comments. You can use this method to force an other encoding.
* Note : the encoding used is not stored in a zip file, not using UTF-8 may lead to encoding issues.
* The function takes a string and returns a bytes array (Uint8Array or Array).
*/
encodeFileName?(name: string): Buffer;
}
}
interface JSZip {
files: {[key: string]: JSZip.JSZipObject};
/**
* Get a file from the archive
*
* @param Path relative path to file
* @return File matching path, null if no file found
*/
file(path: string): JSZip.JSZipObject | null;
/**
* Get files matching a RegExp from archive
*
* @param path RegExp to match
* @return Return all matching files or an empty array
*/
file(path: RegExp): JSZip.JSZipObject[];
/**
* Add a file to the archive
*
* @param path Relative path to file
* @param data Content of the file
* @param options Optional information about the file
* @return JSZip object
*/
file<T extends JSZip.InputType>(path: string, data: InputByType[T] | Promise<InputByType[T]>, options?: JSZip.JSZipFileOptions): this;
file<T extends JSZip.InputType>(path: string, data: null, options?: JSZip.JSZipFileOptions & { dir: true }): this;
/**
* Returns an new JSZip instance with the given folder as root
*
* @param name Name of the folder
* @return New JSZip object with the given folder as root or null
*/
folder(name: string): JSZip | null;
/**
* Returns new JSZip instances with the matching folders as root
*
* @param name RegExp to match
* @return New array of JSZipFile objects which match the RegExp
*/
folder(name: RegExp): JSZip.JSZipObject[];
/**
* Call a callback function for each entry at this folder level.
*
* @param callback function
*/
forEach(callback: (relativePath: string, file: JSZip.JSZipObject) => void): void;
/**
* Get all files which match the given filter function
*
* @param predicate Filter function
* @return Array of matched elements
*/
filter(predicate: (relativePath: string, file: JSZip.JSZipObject) => boolean): JSZip.JSZipObject[];
/**
* Removes the file or folder from the archive
*
* @param path Relative path of file or folder
* @return Returns the JSZip instance
*/
remove(path: string): JSZip;
/**
* Generates a new archive asynchronously
*
* @param options Optional options for the generator
* @param onUpdate The optional function called on each internal update with the metadata.
* @return The serialized archive
*/
generate<T extends JSZip.OutputType>(options?: JSZip.JSZipGeneratorOptions<T>, onUpdate?: JSZip.OnUpdateCallback): OutputByType[T];
/**
* Generates a new archive
*
* @param options Optional options for the generator
* @param onUpdate The optional function called on each internal update with the metadata.
* @return A Node.js `ReadableStream`
*/
generateNodeStream(options?: JSZip.JSZipGeneratorOptions<'nodebuffer'>, onUpdate?: JSZip.OnUpdateCallback): NodeJS.ReadableStream;
/**
* Generates the complete zip file with the internal stream implementation
*
* @param options Optional options for the generator
* @return a StreamHelper
*/
generateInternalStream<T extends JSZip.OutputType>(options?: JSZip.JSZipGeneratorOptions<T>): JSZip.JSZipStreamHelper<OutputByType[T]>;
/**
* Deserialize zip file asynchronously
*
* @param data Serialized zip file
* @param options Options for deserializing
* @return Returns promise
*/
load(data: InputFileFormat, options?: JSZip.JSZipLoadOptions): JSZip;
/**
* Create JSZip instance
*/
new(): this;
(): JSZip;
prototype: JSZip;
support: JSZipSupport;
external: {
Promise: PromiseConstructorLike;
};
version: string;
}
declare var JSZip: JSZip;
export = JSZip;
import PizZip from "./index.js";
import { expectType, expectError } from "tsd";
import * as fs from "fs";
const zip = new PizZip();
const zip1 = new PizZip();
zip.file(
"smile.gif",
"R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs=",
{ base64: true }
zip1.file(
"smile.gif",
"R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs=",
{ base64: true }
);
expectError(zip.foo(
"xxx"
));
expectError(zip1.foo("xxx"));

@@ -22,9 +21,80 @@ var zip3 = new PizZip();

const output = zip.generate({
type: "blob",
mimeType:
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
compression: "DEFLATE",
const output = zip1.generate({
type: "blob",
mimeType:
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
compression: "DEFLATE",
});
expectError(zip.generate({optionXYZ: true}));
expectError(zip1.generate({ optionXYZ: true }));
const zip = new PizZip();
if (PizZip.support.blob) {
/* amazing */
}
new PizZip("data", {
base64: true,
checkCRC32: true,
createFolders: true,
optimizedBinaryString: true,
decodeFileName: (a) => "",
});
zip
.file("Hello.txt", "Hello World\n")
.file(
"smile.gif",
"R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs=",
{ base64: true }
)
.file("smile.gif", fs.readFileSync("smile.gif"))
.file("Xmas.txt", "Ho ho ho !", {
date: new Date("December 25, 2007 00:00:01"),
})
.file("folder/file.txt", "file in folder", { createFolders: true })
.file("script.sh", "echo 'hello world'", { unixPermissions: "755" })
.folder("css")
.remove("css");
zip.load("content", { decodeFileName: (bytes) => "encoding" });
zip.file(/file/).map((z) => z);
zip.folder(/^vid/).map((z) => z);
zip.filter((relativePath, file) => true).map((z) => z);
zip.folder("subfolder").load("data");
const file = zip.file("");
if (file) {
const nameTest: string = file.name;
const textTest: string = file.asText();
const binaryTest: string = file.asBinary();
const arrBufTest: ArrayBuffer = file.asArrayBuffer();
const uint8Test: Uint8Array = file.asUint8Array();
const bufTest: Buffer = file.asNodeBuffer();
}
const noOptionsTest: string = zip.generate();
const noTypeTest: string = zip.generate({ base64: true });
const b64Test: string = zip.generate({
type: "base64",
compression: "DEFLATE",
});
const stringTest: string = zip.generate({
type: "string",
encodeFileName: (s) => new Buffer(s),
});
const arrBufTest: ArrayBuffer = zip.generate({
type: "arraybuffer",
mimeType: "",
});
const blobTest: Blob = zip.generate({
type: "blob",
compressionOptions: { level: 1 },
});
const bufTest: Buffer = zip.generate({ type: "nodebuffer", platform: "DOS" });
const unit8Test: Uint8Array = zip.generate({
type: "uint8array",
base64: true,
});
{
"name": "pizzip",
"version": "3.1.2",
"version": "3.1.3",
"author": "Edgar Hipp",

@@ -5,0 +5,0 @@ "description": "Create, read and edit .zip files synchronously with Javascript",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc