Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require("fs");
- const path = require("path");
- const { promisify } = require("util");
- const { pipeline } = require("stream");
- const archiver = require("archiver");
- const streamPipeline = promisify(pipeline);
- // Function to extract file ID from Google Drive link
- function extractFileId(driveLink) {
- const match = driveLink.match(/[-\w]{25,}/);
- return match ? match[0] : null;
- }
- // Function to download a single file
- async function downloadFile(driveLink, outputDir) {
- const fileId = extractFileId(driveLink);
- if (!fileId) {
- console.error(`Invalid Google Drive link: ${driveLink}`);
- return null;
- }
- const directDownloadURL = `https://drive.google.com/uc?export=download&id=${fileId}`;
- const response = await fetch(directDownloadURL);
- if (!response.ok) {
- console.error(`Failed to download file: ${driveLink}`);
- return null;
- }
- // Get filename from headers, or use default
- let filename = `file_${fileId}`;
- const contentDisposition = response.headers.get("content-disposition");
- if (contentDisposition) {
- const match = contentDisposition.match(/filename="(.+?)"/);
- if (match) filename = match[1];
- }
- // Ensure file has an extension
- if (!path.extname(filename)) filename += ".bin";
- const filePath = path.join(outputDir, filename);
- const fileStream = fs.createWriteStream(filePath);
- await streamPipeline(response.body, fileStream);
- console.log(`Downloaded: ${filename}`);
- return filePath;
- }
- // Function to zip all downloaded files
- function zipFiles(files, outputZipPath) {
- return new Promise((resolve, reject) => {
- const output = fs.createWriteStream(outputZipPath);
- const archive = archiver("zip", { zlib: { level: 9 } });
- output.on("close", () => {
- console.log(`Zipped files: ${outputZipPath}`);
- resolve();
- });
- archive.on("error", (err) => reject(err));
- archive.pipe(output);
- files.forEach((file) => archive.file(file, { name: path.basename(file) }));
- archive.finalize();
- });
- }
- // Main function to download files and zip them
- async function downloadAndZipFiles(links, outputDir, zipFileName) {
- if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
- const downloadedFiles = [];
- for (const link of links) {
- const filePath = await downloadFile(link, outputDir);
- if (filePath) downloadedFiles.push(filePath);
- }
- if (downloadedFiles.length > 0) {
- const zipPath = path.join(outputDir, zipFileName);
- await zipFiles(downloadedFiles, zipPath);
- } else {
- console.log("No files were downloaded. Skipping zip.");
- }
- }
- // Example usage
- const googleDriveLinks = [
- "https://drive.google.com/file/d/10N5oXw0yKu1QuTcXcPEU9-RIXcw8nisu/view?usp=drive_link",
- "https://drive.google.com/file/d/10N5oXw0yKu1QuTcXcPEU9-RIXcw8nisu/view?usp=drive_link",
- ];
- const outputDir = path.join(__dirname, "downloads");
- const zipFileName = "files.zip";
- downloadAndZipFiles(googleDriveLinks, outputDir, zipFileName);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement