question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there anyway to override the date (Checksum of zip mismatched) ?

See original GitHub issue

I’m writing a file to zip on upload:

/**
   * @method compressAndHash
   * @description Returns an object that has the compressed file in a buffer, and the representing md5 checksum
   * @param buffer
   * @param fileName The name of the output file in the zip
   */
  private compressAndHash(buffer: Buffer, fileName: string) {
    const zip = new admZip();
    zip.addFile(fileName, buffer);
    const bufferedZip = zip.toBuffer();
    return {
      compressedFile: bufferedZip,
      hash: this.createHash(bufferedZip)
    };
  }

  /**
   * @method createHash
   * @description Generates md5 for a given buffer
   * @param buffer
   */
  private createHash(buffer: Buffer) {
    const hash = crypt
      .createHash('md5')
      .update(buffer)
      .digest('base64');
    return hash;
  }
}

But it looks like the “date” is part of the checksum. Is there anyway to override the date? Similar to the issue/solution mentioned in https://github.com/archiverjs/node-archiver/issues/82 so we can get consistent checksums?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:8

github_iconTop GitHub Comments

4reactions
pxlprfctcommented, Feb 8, 2019

Hi @ClaytonAstrom,

I recently ran into this exact issue. I ended up mocking the date use in date.Now() which is used inside amd-zip. We now get a consistent now get a checksum.

Apparently dates are apart of the zip spec, and adm-zip isn’t doing anything out of the ordinary here.

Hopefully this is useful! Thanks, Ollie

describe('/zip/', ()=> {
    let clock;

    // date.now() needs to be mocked, as it's stored in the output zip and causes the zips to differ ever-so-slightly
    beforeEach(async function() {
        const date = new Date(2013, 3, 1);
        clock = sinon.useFakeTimers(date.getTime());
    });

    afterEach(function() {
        clock.restore();
    });

    it('zipped data exactly matches expected output', async ()=> {
        const result = zip.zipFile(xml);

        expect(result).to.equal(encodedZip);
    });
});
2reactions
5saviahvcommented, May 2, 2019

adm-zip wraps internal structures so you cant see them directly. one idea is after you call:

zip.addfile(filename, ...);

you also call

zip.getEntry(filename).header.time = new Date("some known date");

and after that you call toBuffer()

Read more comments on GitHub >

github_iconTop Results From Across the Web

Zip File md5 Checksum - Every Time Different
Reason is that zip file contains at least timestamp information about files. And this is what change you md5sum. Every zip entry is...
Read more >
Why doesn't changing a file's name change its checksum?
Changing the file's name (or ownership or timestamps or permission etc.) or accessing it via one of its other names or symbolic links,...
Read more >
Checksum with no creation date - Google Groups
Hi I want to implement a jar comparation files application using a checksum. Two jar files should be the same if both have...
Read more >
Md5 checksum different after gunzip and gzip - Super User
The standard Unix file utility gives you some basic info about a .gz file, e.g.: $ file foo.gz foo.gz: gzip compressed data, was...
Read more >
Different md5sums for same tar contents - Server Fault
Show the tar commandline that you are using. There shouldn't be any difference unless the files are being modified. Even touch filename which ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found