File permissions changed while unzipping.
See original GitHub issueYauzl library is incorrectly unzipping my file permissions. Below is a test file.
const AdmZip = require('adm-zip')
const fs = require('fs-extra')
const path = require('path')
const yauzl = require('yauzl')
const yazl = require('yazl')
// Create an archive of a file whose permission is 755.
let createArchive = function () {
return new Promise(function (resolve) {
let zip = new yazl.ZipFile()
zip.addFile('./node_modules/webpack-dev-server/bin/webpack-dev-server.js', 'webpack-dev-server.js')
zip.end(function () {
zip.outputStream
.pipe(fs.createWriteStream('./test.zip'))
.on('close', function () {
resolve()
})
})
})
}
// Check the permission of the file inside zip. It should be same as what we archived.
let checkArchive = function () {
return new Promise(function (resolve) {
let outputZip = new AdmZip('./test.zip')
resolve(outputZip.getEntry('webpack-dev-server.js'))
})
}
// Extract the file and check its permission. It should be the same as the original permission.
let extractArchive = function () {
return new Promise(function (resolve) {
yauzl.open('./test.zip', {lazyEntries: true}, (error, zip) => {
zip.on('end', () => { resolve() })
zip.on('entry', (entry) => {
zip.openReadStream(entry, function (error, readStream) {
if (error) return zip.emit('error', error)
// Save current entry. Then read next.
fs.ensureFile(path.join('./', entry.fileName), (error) => {
if (error) return zip.emit('error', error)
let outputStream = fs.createWriteStream('./' + entry.fileName)
outputStream.on('error', (error) => { zip.emit('error', error) })
outputStream.on('finish', () => { zip.readEntry() })
readStream.on('error', (error) => { zip.emit('error', error) })
readStream.pipe(outputStream)
})
})
})
zip.readEntry()
})
})
}
let modes = {}
let originalMode = fs.statSync('./node_modules/webpack-dev-server/bin/webpack-dev-server.js')
modes['original'] = ((originalMode.mode << 16) >>> 0)
createArchive()
.then(function () {
return checkArchive()
})
.then(function (permission) {
modes['zipped'] = permission.header.attr
return extractArchive()
})
.then(function () {
let unzippedMode = fs.statSync('./webpack-dev-server.js')
modes['unzipped'] = ((unzippedMode.mode << 16) >>> 0)
console.log('Original ' + modes['original']) // 33261 i.e. 755
console.log('Zipped ' + modes['zipped']) // 33261 i.e. 755
console.log('Unzipped ' + modes['unzipped']) // 33188 i.e. 644
})
Output
Original 2179792896
Zipped 2179792896
Unzipped 2175008768
I have also verified that in Line 287, the same value as zipped
is read, so I am not sure why the file is not set with correct permissions.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Unzipping file whilst getting correct permissions? - Super User
Usually, the Unix unzip restores file permissions. It may be that the ZIP file has been created without file permissions stored (probably with ......
Read more >How can I change the permission on a compressed zipped ...
As far as permissions are concerned, first of all you need to take ownership of the folder and then assign rights or permission...
Read more >Change permissions on a file in a zip without unzipping
For clarification, I am not asking to change the contents of the file, but the Unix permissions that are set on one. I...
Read more >Maintain file permissions while unzipping in clojure
I am using ZipInputStream and BufferedOutputStream from Java APIs, to read and write respectively. I don't think there's anything wrong with my ...
Read more >unzip and change file permissions - UNIX and Linux Forums
Hi, Is there any way to unzip a zip file and change file permissions to 777 while extracting the files. Does the file...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ah sorry! I was thinking of the docs for yazl. My mistake.
If you trust that your zip files are all coming from a UNIX-like zipfile creator, then you can use this:
I have not done an analysis to see how common it is to encode the permission bits like this in the external file attributes, but I know it is typical in at least some situations. The meaning of the external file attributes is not specified by the zipfile specification.
It’s probably appropriate for me to do this investigation and have a getter function in the Entry class like I do for
getLastModDate()
. An implementation would include the above logic in at least one case for that function.I opened #102 to implement the method.