Skip to content

added Compress to ZIP code folder #1344

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions JavaScript/Compress to ZIP/README.md
Original file line number Diff line number Diff line change
@@ -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
75 changes: 75 additions & 0 deletions JavaScript/Compress to ZIP/compress.js
Original file line number Diff line number Diff line change
@@ -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}
112 changes: 112 additions & 0 deletions JavaScript/Compress to ZIP/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions JavaScript/Compress to ZIP/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
1 change: 1 addition & 0 deletions JavaScript/Compress to ZIP/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is a text file not in a test folder
1 change: 1 addition & 0 deletions JavaScript/Compress to ZIP/testfolder/anothertext.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is folder text file that will be compressed