Advertisement
pvcodes

Untitled

Mar 18th, 2025
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require("fs");
  2. const path = require("path");
  3. const { promisify } = require("util");
  4. const { pipeline } = require("stream");
  5. const archiver = require("archiver");
  6.  
  7. const streamPipeline = promisify(pipeline);
  8.  
  9. // Function to extract file ID from Google Drive link
  10. function extractFileId(driveLink) {
  11.     const match = driveLink.match(/[-\w]{25,}/);
  12.     return match ? match[0] : null;
  13. }
  14.  
  15. // Function to download a single file
  16. async function downloadFile(driveLink, outputDir) {
  17.     const fileId = extractFileId(driveLink);
  18.     if (!fileId) {
  19.         console.error(`Invalid Google Drive link: ${driveLink}`);
  20.         return null;
  21.     }
  22.  
  23.     const directDownloadURL = `https://drive.google.com/uc?export=download&id=${fileId}`;
  24.     const response = await fetch(directDownloadURL);
  25.  
  26.     if (!response.ok) {
  27.         console.error(`Failed to download file: ${driveLink}`);
  28.         return null;
  29.     }
  30.  
  31.     // Get filename from headers, or use default
  32.     let filename = `file_${fileId}`;
  33.     const contentDisposition = response.headers.get("content-disposition");
  34.     if (contentDisposition) {
  35.         const match = contentDisposition.match(/filename="(.+?)"/);
  36.         if (match) filename = match[1];
  37.     }
  38.  
  39.     // Ensure file has an extension
  40.     if (!path.extname(filename)) filename += ".bin";
  41.  
  42.     const filePath = path.join(outputDir, filename);
  43.     const fileStream = fs.createWriteStream(filePath);
  44.     await streamPipeline(response.body, fileStream);
  45.  
  46.     console.log(`Downloaded: ${filename}`);
  47.     return filePath;
  48. }
  49.  
  50. // Function to zip all downloaded files
  51. function zipFiles(files, outputZipPath) {
  52.     return new Promise((resolve, reject) => {
  53.         const output = fs.createWriteStream(outputZipPath);
  54.         const archive = archiver("zip", { zlib: { level: 9 } });
  55.  
  56.         output.on("close", () => {
  57.             console.log(`Zipped files: ${outputZipPath}`);
  58.             resolve();
  59.         });
  60.  
  61.         archive.on("error", (err) => reject(err));
  62.  
  63.         archive.pipe(output);
  64.         files.forEach((file) => archive.file(file, { name: path.basename(file) }));
  65.         archive.finalize();
  66.     });
  67. }
  68.  
  69. // Main function to download files and zip them
  70. async function downloadAndZipFiles(links, outputDir, zipFileName) {
  71.     if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
  72.  
  73.     const downloadedFiles = [];
  74.     for (const link of links) {
  75.         const filePath = await downloadFile(link, outputDir);
  76.         if (filePath) downloadedFiles.push(filePath);
  77.     }
  78.  
  79.     if (downloadedFiles.length > 0) {
  80.         const zipPath = path.join(outputDir, zipFileName);
  81.         await zipFiles(downloadedFiles, zipPath);
  82.     } else {
  83.         console.log("No files were downloaded. Skipping zip.");
  84.     }
  85. }
  86.  
  87. // Example usage
  88. const googleDriveLinks = [
  89.     "https://drive.google.com/file/d/10N5oXw0yKu1QuTcXcPEU9-RIXcw8nisu/view?usp=drive_link",
  90.     "https://drive.google.com/file/d/10N5oXw0yKu1QuTcXcPEU9-RIXcw8nisu/view?usp=drive_link",
  91. ];
  92.  
  93. const outputDir = path.join(__dirname, "downloads");
  94. const zipFileName = "files.zip";
  95.  
  96. downloadAndZipFiles(googleDriveLinks, outputDir, zipFileName);
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement