diff --git a/JavaScript/Compress to ZIP/README.md b/JavaScript/Compress to ZIP/README.md new file mode 100644 index 000000000..dc33f4616 --- /dev/null +++ b/JavaScript/Compress to ZIP/README.md @@ -0,0 +1,7 @@ +# Compress-to-ZIP + +## Compress any file or folder to .zip format + +Visit the npm page for more information + +npm page - https://www.npmjs.com/package/compress-to-zip \ No newline at end of file diff --git a/JavaScript/Compress to ZIP/compress.js b/JavaScript/Compress to ZIP/compress.js new file mode 100644 index 000000000..f28805cf0 --- /dev/null +++ b/JavaScript/Compress to ZIP/compress.js @@ -0,0 +1,75 @@ +const zlib = require("zip-lib"); +const path = require("path") + +//compress single file + +const compressSingleFile = async (sourceFilePath , compressFilePath) =>{ + + try { + await zlib.archiveFile(sourceFilePath,compressFilePath) + console.log("Successfully compressed the file!"); + + } catch (error) { + console.log("Oops! something happened "); + console.log("Error message" + error.message); + } +} + + +//try something like below ----- +// compressSingleFile("./test.txt","./test-compress.zip") + + + +const compressSingleFolder = async (sourceFolderPath , compressFolderPath) =>{ + + try { + await zlib.archiveFolder(sourceFolderPath,compressFolderPath) + console.log("Successfully compressed the Folder " + path.basename(sourceFolderPath)); + + } catch (error) { + console.log("Oops! something happened "); + console.log("Error message" + error.message); + } +} + +//try something like below ----- +//compressSingleFolder("./testfolder","./testfolder-compress.zip") + + +const compressMultiple = async ({filePaths,folderPaths,compressPath}) =>{ + + try { + + const zip = new zlib.Zip(); + if(filePaths.length!==0){ + filePaths.forEach(async path=>{ + await zip.addFile(path); + }) + } + + if(folderPaths.length!==0){ + folderPaths.forEach(async path=>{ + await zip.addFolder(path); + }) + } + + await zip.archive(compressPath) + console.log("Successfully compressed as " + path.basename(compressPath)); + } catch (error) { + console.log("Oops! something happened "); + console.log("Error message" + error.message); + } +} + + +//try something like below ----- + +// compressMultiple({ +// filePaths:["./test.txt"], +// folderPaths :["./testfolder"], +// compressPath:"./compressed.zip" +// }) + + +module.exports = {compressSingleFile,compressSingleFolder,compressMultiple} diff --git a/JavaScript/Compress to ZIP/package-lock.json b/JavaScript/Compress to ZIP/package-lock.json new file mode 100644 index 000000000..1da6934b3 --- /dev/null +++ b/JavaScript/Compress to ZIP/package-lock.json @@ -0,0 +1,112 @@ +{ + "name": "compress-to-zip", + "version": "1.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "compress-to-zip", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "zip-lib": "^0.7.3" + } + }, + "node_modules/buffer-crc32": { + "version": "0.2.13", + "resolved": "https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.13.tgz", + "integrity": "sha1-DTM+PwDqxQqhRUq9MO+MKl2ackI=", + "engines": { + "node": "*" + } + }, + "node_modules/fd-slicer": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/fd-slicer/-/fd-slicer-1.1.0.tgz", + "integrity": "sha1-JcfInLH5B3+IkbvmHY85Dq4lbx4=", + "dependencies": { + "pend": "~1.2.0" + } + }, + "node_modules/pend": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz", + "integrity": "sha1-elfrVQpng/kRUzH89GY9XI4AelA=" + }, + "node_modules/yauzl": { + "version": "2.10.0", + "resolved": "https://registry.npmjs.org/yauzl/-/yauzl-2.10.0.tgz", + "integrity": "sha1-x+sXyT4RLLEIb6bY5R+wZnt5pfk=", + "dependencies": { + "buffer-crc32": "~0.2.3", + "fd-slicer": "~1.1.0" + } + }, + "node_modules/yazl": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/yazl/-/yazl-2.5.1.tgz", + "integrity": "sha512-phENi2PLiHnHb6QBVot+dJnaAZ0xosj7p3fWl+znIjBDlnMI2PsZCJZ306BPTFOaHf5qdDEI8x5qFrSOBN5vrw==", + "dependencies": { + "buffer-crc32": "~0.2.3" + } + }, + "node_modules/zip-lib": { + "version": "0.7.3", + "resolved": "https://registry.npmjs.org/zip-lib/-/zip-lib-0.7.3.tgz", + "integrity": "sha512-hp40KYzTJvoaCRr2t6hztlPnVmHYqDUDiIn0hlfAFwVBs3/jwkLy8aZ7NVGHECeWH2Tv8WPwWyR6QuWYarIjJQ==", + "dependencies": { + "yauzl": "^2.10.0", + "yazl": "^2.5.1" + }, + "engines": { + "node": ">=8" + } + } + }, + "dependencies": { + "buffer-crc32": { + "version": "0.2.13", + "resolved": "https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.13.tgz", + "integrity": "sha1-DTM+PwDqxQqhRUq9MO+MKl2ackI=" + }, + "fd-slicer": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/fd-slicer/-/fd-slicer-1.1.0.tgz", + "integrity": "sha1-JcfInLH5B3+IkbvmHY85Dq4lbx4=", + "requires": { + "pend": "~1.2.0" + } + }, + "pend": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz", + "integrity": "sha1-elfrVQpng/kRUzH89GY9XI4AelA=" + }, + "yauzl": { + "version": "2.10.0", + "resolved": "https://registry.npmjs.org/yauzl/-/yauzl-2.10.0.tgz", + "integrity": "sha1-x+sXyT4RLLEIb6bY5R+wZnt5pfk=", + "requires": { + "buffer-crc32": "~0.2.3", + "fd-slicer": "~1.1.0" + } + }, + "yazl": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/yazl/-/yazl-2.5.1.tgz", + "integrity": "sha512-phENi2PLiHnHb6QBVot+dJnaAZ0xosj7p3fWl+znIjBDlnMI2PsZCJZ306BPTFOaHf5qdDEI8x5qFrSOBN5vrw==", + "requires": { + "buffer-crc32": "~0.2.3" + } + }, + "zip-lib": { + "version": "0.7.3", + "resolved": "https://registry.npmjs.org/zip-lib/-/zip-lib-0.7.3.tgz", + "integrity": "sha512-hp40KYzTJvoaCRr2t6hztlPnVmHYqDUDiIn0hlfAFwVBs3/jwkLy8aZ7NVGHECeWH2Tv8WPwWyR6QuWYarIjJQ==", + "requires": { + "yauzl": "^2.10.0", + "yazl": "^2.5.1" + } + } + } +} diff --git a/JavaScript/Compress to ZIP/package.json b/JavaScript/Compress to ZIP/package.json new file mode 100644 index 000000000..323ba3a86 --- /dev/null +++ b/JavaScript/Compress to ZIP/package.json @@ -0,0 +1,15 @@ +{ + "name": "compress-to-zip", + "version": "1.0.0", + "description": "", + "main": "compress.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": ["zip","zlib","compress"], + "author": "Anirban Pratihar", + "license": "ISC", + "dependencies": { + "zip-lib": "^0.7.3" + } +} diff --git a/JavaScript/Compress to ZIP/test.txt b/JavaScript/Compress to ZIP/test.txt new file mode 100644 index 000000000..6def37030 --- /dev/null +++ b/JavaScript/Compress to ZIP/test.txt @@ -0,0 +1 @@ +this is a text file not in a test folder \ No newline at end of file diff --git a/JavaScript/Compress to ZIP/testfolder/anothertext.txt b/JavaScript/Compress to ZIP/testfolder/anothertext.txt new file mode 100644 index 000000000..09f36f566 --- /dev/null +++ b/JavaScript/Compress to ZIP/testfolder/anothertext.txt @@ -0,0 +1 @@ +This is folder text file that will be compressed \ No newline at end of file