Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ignoring Language Encoding flag #521

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion dist/zip-fs-full.js
Original file line number Diff line number Diff line change
Expand Up @@ -10082,6 +10082,7 @@
const bufferedWrite = getOptionValue(zipWriter, options, "bufferedWrite");
const dataDescriptorSignature = getOptionValue(zipWriter, options, "dataDescriptorSignature", false);
const signal = getOptionValue(zipWriter, options, "signal");
const useUnicodeFileNames = getOptionValue(zipWriter, options, "useUnicodeFileNames", true);
const useCompressionStream = getOptionValue(zipWriter, options, "useCompressionStream");
let dataDescriptor = getOptionValue(zipWriter, options, "dataDescriptor", true);
let zip64 = getOptionValue(zipWriter, options, PROPERTY_NAME_ZIP64);
Expand Down Expand Up @@ -10163,6 +10164,7 @@
zipCrypto,
bufferedWrite,
keepOrder,
useUnicodeFileNames,
dataDescriptor,
dataDescriptorSignature,
signal,
Expand Down Expand Up @@ -10501,6 +10503,7 @@
level,
zip64,
zipCrypto,
useUnicodeFileNames,
dataDescriptor,
directory,
rawExtraField,
Expand Down Expand Up @@ -10557,7 +10560,10 @@
} else {
rawExtraFieldNTFS = rawExtraFieldExtendedTimestamp = new Uint8Array();
}
let bitFlag = BITFLAG_LANG_ENCODING_FLAG;
let bitFlag = 0;
if (useUnicodeFileNames) {
bitFlag = bitFlag | BITFLAG_LANG_ENCODING_FLAG;
}
if (dataDescriptor) {
bitFlag = bitFlag | BITFLAG_DATA_DESCRIPTOR;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/zip-fs-full.min.js

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion dist/zip-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4227,6 +4227,7 @@
const bufferedWrite = getOptionValue(zipWriter, options, "bufferedWrite");
const dataDescriptorSignature = getOptionValue(zipWriter, options, "dataDescriptorSignature", false);
const signal = getOptionValue(zipWriter, options, "signal");
const useUnicodeFileNames = getOptionValue(zipWriter, options, "useUnicodeFileNames", true);
const useCompressionStream = getOptionValue(zipWriter, options, "useCompressionStream");
let dataDescriptor = getOptionValue(zipWriter, options, "dataDescriptor", true);
let zip64 = getOptionValue(zipWriter, options, PROPERTY_NAME_ZIP64);
Expand Down Expand Up @@ -4308,6 +4309,7 @@
zipCrypto,
bufferedWrite,
keepOrder,
useUnicodeFileNames,
dataDescriptor,
dataDescriptorSignature,
signal,
Expand Down Expand Up @@ -4646,6 +4648,7 @@
level,
zip64,
zipCrypto,
useUnicodeFileNames,
dataDescriptor,
directory,
rawExtraField,
Expand Down Expand Up @@ -4702,7 +4705,10 @@
} else {
rawExtraFieldNTFS = rawExtraFieldExtendedTimestamp = new Uint8Array();
}
let bitFlag = BITFLAG_LANG_ENCODING_FLAG;
let bitFlag = 0;
if (useUnicodeFileNames) {
bitFlag = bitFlag | BITFLAG_LANG_ENCODING_FLAG;
}
if (dataDescriptor) {
bitFlag = bitFlag | BITFLAG_DATA_DESCRIPTOR;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/zip-fs.min.js

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion dist/zip-full.js
Original file line number Diff line number Diff line change
Expand Up @@ -8450,6 +8450,7 @@
const bufferedWrite = getOptionValue(zipWriter, options, "bufferedWrite");
const dataDescriptorSignature = getOptionValue(zipWriter, options, "dataDescriptorSignature", false);
const signal = getOptionValue(zipWriter, options, "signal");
const useUnicodeFileNames = getOptionValue(zipWriter, options, "useUnicodeFileNames", true);
const useCompressionStream = getOptionValue(zipWriter, options, "useCompressionStream");
let dataDescriptor = getOptionValue(zipWriter, options, "dataDescriptor", true);
let zip64 = getOptionValue(zipWriter, options, PROPERTY_NAME_ZIP64);
Expand Down Expand Up @@ -8531,6 +8532,7 @@
zipCrypto,
bufferedWrite,
keepOrder,
useUnicodeFileNames,
dataDescriptor,
dataDescriptorSignature,
signal,
Expand Down Expand Up @@ -8869,6 +8871,7 @@
level,
zip64,
zipCrypto,
useUnicodeFileNames,
dataDescriptor,
directory,
rawExtraField,
Expand Down Expand Up @@ -8925,7 +8928,10 @@
} else {
rawExtraFieldNTFS = rawExtraFieldExtendedTimestamp = new Uint8Array();
}
let bitFlag = BITFLAG_LANG_ENCODING_FLAG;
let bitFlag = 0;
if (useUnicodeFileNames) {
bitFlag = bitFlag | BITFLAG_LANG_ENCODING_FLAG;
}
if (dataDescriptor) {
bitFlag = bitFlag | BITFLAG_DATA_DESCRIPTOR;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/zip-full.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/zip-no-worker-deflate.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/zip-no-worker.min.js

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion dist/zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -4227,6 +4227,7 @@
const bufferedWrite = getOptionValue(zipWriter, options, "bufferedWrite");
const dataDescriptorSignature = getOptionValue(zipWriter, options, "dataDescriptorSignature", false);
const signal = getOptionValue(zipWriter, options, "signal");
const useUnicodeFileNames = getOptionValue(zipWriter, options, "useUnicodeFileNames", true);
const useCompressionStream = getOptionValue(zipWriter, options, "useCompressionStream");
let dataDescriptor = getOptionValue(zipWriter, options, "dataDescriptor", true);
let zip64 = getOptionValue(zipWriter, options, PROPERTY_NAME_ZIP64);
Expand Down Expand Up @@ -4308,6 +4309,7 @@
zipCrypto,
bufferedWrite,
keepOrder,
useUnicodeFileNames,
dataDescriptor,
dataDescriptorSignature,
signal,
Expand Down Expand Up @@ -4646,6 +4648,7 @@
level,
zip64,
zipCrypto,
useUnicodeFileNames,
dataDescriptor,
directory,
rawExtraField,
Expand Down Expand Up @@ -4702,7 +4705,10 @@
} else {
rawExtraFieldNTFS = rawExtraFieldExtendedTimestamp = new Uint8Array();
}
let bitFlag = BITFLAG_LANG_ENCODING_FLAG;
let bitFlag = 0;
if (useUnicodeFileNames) {
bitFlag = bitFlag | BITFLAG_LANG_ENCODING_FLAG;
}
if (dataDescriptor) {
bitFlag = bitFlag | BITFLAG_DATA_DESCRIPTOR;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/zip.min.js

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -10076,6 +10076,7 @@ async function addFile(zipWriter, name, reader, options) {
const bufferedWrite = getOptionValue(zipWriter, options, "bufferedWrite");
const dataDescriptorSignature = getOptionValue(zipWriter, options, "dataDescriptorSignature", false);
const signal = getOptionValue(zipWriter, options, "signal");
const useUnicodeFileNames = getOptionValue(zipWriter, options, "useUnicodeFileNames", true);
const useCompressionStream = getOptionValue(zipWriter, options, "useCompressionStream");
let dataDescriptor = getOptionValue(zipWriter, options, "dataDescriptor", true);
let zip64 = getOptionValue(zipWriter, options, PROPERTY_NAME_ZIP64);
Expand Down Expand Up @@ -10157,6 +10158,7 @@ async function addFile(zipWriter, name, reader, options) {
zipCrypto,
bufferedWrite,
keepOrder,
useUnicodeFileNames,
dataDescriptor,
dataDescriptorSignature,
signal,
Expand Down Expand Up @@ -10495,6 +10497,7 @@ function getHeaderInfo(options) {
level,
zip64,
zipCrypto,
useUnicodeFileNames,
dataDescriptor,
directory,
rawExtraField,
Expand Down Expand Up @@ -10551,7 +10554,10 @@ function getHeaderInfo(options) {
} else {
rawExtraFieldNTFS = rawExtraFieldExtendedTimestamp = new Uint8Array();
}
let bitFlag = BITFLAG_LANG_ENCODING_FLAG;
let bitFlag = 0;
if (useUnicodeFileNames) {
bitFlag = bitFlag | BITFLAG_LANG_ENCODING_FLAG;
}
if (dataDescriptor) {
bitFlag = bitFlag | BITFLAG_DATA_DESCRIPTOR;
}
Expand Down
10 changes: 9 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,15 @@ export interface ZipWriterConstructorOptions {
*/
versionMadeBy?: number;
/**
* `true` to to add a data descriptor.
* `true` to mark the file names as UTF-8 setting the general purpose bit 11 in the header (see Appendix D - Language Encoding (EFS)), `false` to mark the names as compliant with the original IBM Code Page 437.
*
* Note that this does not ensure that the file names are in the correct encoding.
*
* @defaultValue true
*/
useUnicodeFileNames?: boolean;
/**
* `true` to add a data descriptor.
*
* When set to `false`, the {@link ZipWriterConstructorOptions#bufferedWrite} option will automatically be set to `true`.
*
Expand Down
2 changes: 1 addition & 1 deletion index.min.js

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion lib/core/zip-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ async function addFile(zipWriter, name, reader, options) {
const bufferedWrite = getOptionValue(zipWriter, options, "bufferedWrite");
const dataDescriptorSignature = getOptionValue(zipWriter, options, "dataDescriptorSignature", false);
const signal = getOptionValue(zipWriter, options, "signal");
const useUnicodeFileNames = getOptionValue(zipWriter, options, "useUnicodeFileNames", true);
const useCompressionStream = getOptionValue(zipWriter, options, "useCompressionStream");
let dataDescriptor = getOptionValue(zipWriter, options, "dataDescriptor", true);
let zip64 = getOptionValue(zipWriter, options, PROPERTY_NAME_ZIP64);
Expand Down Expand Up @@ -346,6 +347,7 @@ async function addFile(zipWriter, name, reader, options) {
zipCrypto,
bufferedWrite,
keepOrder,
useUnicodeFileNames,
dataDescriptor,
dataDescriptorSignature,
signal,
Expand Down Expand Up @@ -684,6 +686,7 @@ function getHeaderInfo(options) {
level,
zip64,
zipCrypto,
useUnicodeFileNames,
dataDescriptor,
directory,
rawExtraField,
Expand Down Expand Up @@ -740,7 +743,10 @@ function getHeaderInfo(options) {
} else {
rawExtraFieldNTFS = rawExtraFieldExtendedTimestamp = new Uint8Array();
}
let bitFlag = BITFLAG_LANG_ENCODING_FLAG;
let bitFlag = 0;
if (useUnicodeFileNames) {
bitFlag = bitFlag | BITFLAG_LANG_ENCODING_FLAG;
}
if (dataDescriptor) {
bitFlag = bitFlag | BITFLAG_DATA_DESCRIPTOR;
}
Expand Down
44 changes: 44 additions & 0 deletions tests/all/test-use-unicode-file-names.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* global Blob */

import * as zip from "../../index.js";

const TEXT_CONTENT = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.";
const FILENAME = "lorem.txt";
const BLOB = new Blob([TEXT_CONTENT], { type: zip.getMimeType(FILENAME) });

export { test };

async function test() {
let blobWriter;
blobWriter = await buildZip();
await assertLanguageEncodingFlagIs(true, blobWriter);

blobWriter = await buildZip({ useUnicodeFileNames: false });
await assertLanguageEncodingFlagIs(false, blobWriter);
}

async function buildZip(options) {
const blobWriter = new zip.BlobWriter("application/zip");

zip.configure({ chunkSize: 128, useWebWorkers: true });
const zipWriter = new zip.ZipWriter(blobWriter, options);
await zipWriter.add(FILENAME, new zip.BlobReader(BLOB));
await zipWriter.close();
return blobWriter;
}

async function assertLanguageEncodingFlagIs(expectedLanguageEncodingFlag, blobWriter) {
const zipReader = new zip.ZipReader(new zip.BlobReader(await blobWriter.getData()));
const entries = await zipReader.getEntries();
const actual = entries[0].bitFlag.languageEncodingFlag;
if (actual === expectedLanguageEncodingFlag) {
const data = await entries[0].getData(new zip.BlobWriter(zip.getMimeType(entries[0].filename)));
await zipReader.close();
await zip.terminateWorkers();
if (TEXT_CONTENT != await data.text() || entries[0].filename != FILENAME || entries[0].uncompressedSize != TEXT_CONTENT.length) {
throw new Error();
}
} else {
throw new Error(`Expected language flag to be ${expectedLanguageEncodingFlag}, but was ${actual}`);
}
}
1 change: 1 addition & 0 deletions tests/tests-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default ([
{ title: "Custom IO classes", script: "./test-custom-io.js" },
{ title: "Data descriptor signature", script: "./test-data-descriptor-signature.js" },
{ title: "Data descriptor", script: "./test-data-descriptor.js" },
{ title: "Unicode file names", script: "./test-use-unicode-file-names.js" },
{ title: "Directory", script: "./test-directory.js" },
{ title: "Duplicated Filename", script: "./test-duplicated-filename.js" },
{ title: "Empty zip file", script: "./test-empty.js" },
Expand Down