-
Notifications
You must be signed in to change notification settings - Fork 38
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
Is it possible to create a zip from an existing file/folder structure? #18
Comments
Someone has a workaround for this? I also need to compress a folder. |
I used zip-folder. |
I also needed such a functionality, but seems like there is no way to do it simply. So I did all folder creation and adding files to zip file using methods from JSZip library on which this library is based. |
@thinkloop @adelriosantiago @kkuatov If your folder structure is not soooo deep, it's easy to walk the folder and add each file to zip. For example: const fs = require('fs');
const path = require('path');
const NodeZip = require('node-zip');
const zip = new NodeZip();
const BUILD_PATH = './build';
const OUTPUT_PATH = path.join(__dirname, `${BUILD_PATH}.zip`);
const ALL_FILES = {};
const isFolder = folderPath => fs.statSync(folderPath).isDirectory();
const folderWalker = (folder = './', lastFolder = './') => {
const folderPath = path.join(__dirname, BUILD_PATH, lastFolder, folder);
fs.readdirSync(folderPath)
.filter(filename => filename[0] !== '.')
.forEach((file) => {
const fullpath = path.resolve(__dirname, BUILD_PATH, lastFolder, folder, file);
if (isFolder(fullpath)) {
const fatherFolder = path.join(lastFolder, folder);
folderWalker(file, fatherFolder);
} else {
const relativePath = path.join(lastFolder, folder, file);
ALL_FILES[fullpath] = relativePath;
}
});
};
const zipFolder = (folder) => {
folderWalker(folder);
Object.keys(ALL_FILES).forEach((fullpath) => {
const relativePath = ALL_FILES[fullpath];
zip.file(relativePath, fs.readFileSync(fullpath));
});
const data = zip.generate({ base64: false, compression: 'DEFLATE' });
fs.writeFileSync(OUTPUT_PATH, data, 'binary');
};
zipFolder(); Check Gist here to see full code. |
Hello, thanks a lot for the lib.
Is it possible to create a zip from existing file/folder structures?
I would like to do something like:
Instead of creating new files and folders from within the zip utility. Is this possible?
The text was updated successfully, but these errors were encountered: