I have created a POC which creates a zip containing a xml file whose characteristics is currently “UTF8”. I want it to be “Descriptor UTF8”. If someone has faced similar issue do assist.
Expected format
Current format
Below mentioned is the code for POC:
const yazl = require('yazl');
const fs = require('fs');
const path = require('path');
function test(
location,
ackId,
responseContent,
responsezipFileName,
responsefileName
) {
return new Promise((resolve, reject) => {
const zipFile = new yazl.ZipFile();
// Ensure the content is a Buffer with UTF-8 encoding
const contentBuffer = Buffer.isBuffer(responseContent) ? responseContent : Buffer.from(responseContent, 'utf8');
// Function to convert Date to DOS date/time format
function dateToDosTime(d) {
return {
date: ((d.getFullYear() - 1980) << 9) | ((d.getMonth() + 1) << 5) | d.getDate(),
time: (d.getHours() << 11) | (d.getMinutes() << 5) | (d.getSeconds() / 2)
};
}
const now = new Date();
const { date: dosDate, time: dosTime } = dateToDosTime(now);
// Add the file to the ZIP, ensuring the filename is UTF-8 encoded
zipFile.addBuffer(contentBuffer, responsefileName, {
mtime: now,
mode: 0o100666, // FAT-compatible mode
compress: true,
forceZip64Format: false,
dosPermissions: 0x20, // Archive bit set (normal file)
lastModFileTime: dosTime,
lastModFileDate: dosDate
});
// Finalize the ZIP file and write to the output location
const outputZipPath = path.join(location, `${responsezipFileName}`);
const writeStream = fs.createWriteStream(outputZipPath);
zipFile.outputStream.pipe(writeStream)
.on('close', () => {
console.info(`${responsezipFileName} zip file created for FAT system.`);
resolve();
})
.on('error', (err) => {
console.error('Error in creating zip file', err);
reject(err);
});
zipFile.end();
}).catch((error) => {
console.error('Error inside test:::', error);
throw new Error(error.message || 'Unknown error occurred');
});
}
// Example usage
test(
'Downloads',
'12345',
`<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>John</to>
<from>Jane</from>
<heading>Reminder</heading>
<body>Don't forget the meeting at 10 AM tomorrow!</body>
<date>2024-09-05</date>
<unicodeExample>😊</unicodeExample>
</note>
`,
'response.zip',
'response.xml'
).then(() => {
console.log('ZIP file creation successful.');
}).catch((err) => {
console.error('ZIP file creation failed:', err);
});
Characteristics of zipped file to be “Descriptor UTF8”. I have tried several zipping libraries, none worked.
5