adm-zip icon indicating copy to clipboard operation
adm-zip copied to clipboard

Error: Invalid CEN header (bad signature)

Open nitinmagdumvista opened this issue 2 years ago • 4 comments

I am getting zip response from a external service which contains multiple files through that zip response i want to get all the files and send to another http post request but i am getting Error: Invalid CEN header (bad signature)

const authStr =Bearer ${token}; const reqBody = logoSignature; const composerUrl = this.configService.getConfiguration('COMPOSER_URL'); const axiosPostConfig: AxiosRequestConfig = { method: 'post', url: composerUrl, headers: { Authorization: authStr, }, data: reqBody, }; let composerServiceResponse = await Utility.apiCall( axiosPostConfig, executionLog, ); console.log('composerServiceResponse', composerServiceResponse); const buffer = Buffer.from(composerServiceResponse,'binary'); const zip = new AdmZip(buffer); const entries = zip.getEntries(); for (let entry of entries) { const buffer = entry.getData(); console.log( 'File: ' + entry.entryName + ', length (bytes): ' + buffer.length + ', contents: ' + buffer.toString('utf-8'), ); }

nitinmagdumvista avatar Jan 26 '23 06:01 nitinmagdumvista

https://stackoverflow.com/questions/45661857/extract-uploaded-file-using-adm-zip

same kind of response i am getting from the API which returning zip file

nitinmagdumvista avatar Jan 26 '23 07:01 nitinmagdumvista

i think you need to add responseType: 'arraybuffer', to your AxiosRequestConfig

cthackers avatar Jan 26 '23 07:01 cthackers

Hi, I have the same error on a AWS lambda. It works well in local though

Thykof avatar Jun 07 '24 22:06 Thykof

This may be related to an issue of the zip headers having a CRLF (new line) at the start of the file. I have an application which creates a zip which does this and another which doesnt for the same file content.

Error: ADM-ZIP: Invalid CEN header (bad signature) at Object.INVALID_CEN
 (/home/site/wwwroot/node_modules/adm-zip/util/errors.js:56:16) at Object.loadFromBinary
 (/home/site/wwwroot/node_modules/adm-zip/headers/entryHeader.js:247:36) at set header [as header]
 (/home/site/wwwroot/node_modules/adm-zip/zipEntry.js:340:28) at readEntries
 (/home/site/wwwroot/node_modules/adm-zip/zipFile.js:66:26) at get entries [as entries]
 (/home/site/wwwroot/node_modules/adm-zip/zipFile.js:145:17) at Object.getEntries
 (/home/site/wwwroot/node_modules/adm-zip/adm-zip.js:636:32)

This is an incredibly hacky approach, but before calling adm-zip, edit the buffer to remove CRLF:

    // Check if the first two bytes are CR (0x0D) and LF (0x0A)
    if (bufferPart[0] === 0x0d && bufferPart[1] === 0x0a) {
      // Create a new buffer without the first two bytes
      bufferPart = bufferPart.slice(2);
      console.log("CR and LF found and removed:", bufferPart.length);
    } else {
      console.log("CR and LF not found:", bufferPart.length);
    }

The way to debug this is to read the first few bytes of the buffer to see what is here. The first 2 characters should be PK

https://github.com/cthackers/adm-zip/blob/master/headers/entryHeader.js#L246

timmo001 avatar Aug 27 '24 11:08 timmo001