File size: 1,674 Bytes
5490062
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
import zipfile
import pandas as pd
from config import settings

class ZipExporter:
    def __init__(self):
        self.base_dir = settings.DOWNLOAD_DIR

    def export_zip(self, metadata_df: pd.DataFrame, output_zip_path: str):
        """Exports all downloaded files into a single ZIP archive."""
        if not os.path.exists(output_zip_path):
            os.makedirs(output_zip_path, exist_ok=True)
        
        print(f"\n📦 Starting ZIP export to: {output_zip_path}")
        
        with zipfile.ZipFile(output_zip_path, 'w', zipfile.ZIP_DEFLATED) as zf:
            
            for index, row in metadata_df.iterrows():
                file_path = row['download_path']
                
                if os.path.exists(file_path):
                    # Add the file to the zip, using only the relative path inside the zip
                    arcname = os.path.relpath(file_path, self.base_dir)
                    zf.write(file_path, arcname)
                    print(f"   - Added: {arcname}")
                else:
                    print(f"   - Warning: File missing during zip export: {file_path}")
        
        print(f"🎉 ZIP Export Complete! Archive saved at: {output_zip_path}")
        return True

    def export_csv(self, metadata_df: pd.DataFrame, output_csv_path: str):
        """Exports the metadata DataFrame to a CSV file."""
        if not metadata_df.empty:
            metadata_df.to_csv(output_csv_path, index=False)
            print(f"💾 CSV Export Complete! Metadata saved to: {output_csv_path}")
            return True
        print("⚠️ Metadata DataFrame is empty. CSV export skipped.")
        return False