async def retrieve_files_as_zip(
        self,
        document_ids: Optional[list[UUID]] = None,
        start_date: Optional[datetime] = None,
        end_date: Optional[datetime] = None,
    ) -> tuple[str, BinaryIO, int]:
        """Retrieve multiple files and return them as a zip file."""

        query = f"""
        SELECT document_id, name, oid, size
        FROM {self._get_table_name(self.table_name)}
        WHERE 1=1
        """
        params: list = []

        if document_ids:
            query += f" AND document_id = ANY(${len(params) + 1})"
            params.append([str(doc_id) for doc_id in document_ids])

        if start_date:
            query += f" AND created_at >= ${len(params) + 1}"
            params.append(start_date)

        if end_date:
            query += f" AND created_at <= ${len(params) + 1}"
            params.append(end_date)

        query += " ORDER BY created_at DESC"

        results = await self.connection_manager.fetch_query(query, params)

        if not results:
            raise R2RException(
                status_code=404,
                message="No files found matching the specified criteria",
            )

        zip_buffer = BytesIO()
        total_size = 0

        async with self.connection_manager.pool.get_connection() as conn:  # type: ignore
            with ZipFile(zip_buffer, "w") as zip_file:
                for record in results:
                    file_content = await self._read_lobject(
                        conn, record["oid"]
                    )

                    zip_file.writestr(record["name"], file_content)
                    total_size += record["size"]

        zip_buffer.seek(0)
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        zip_filename = f"files_export_{timestamp}.zip"

        return zip_filename, zip_buffer, zip_buffer.getbuffer().nbytes
Retrieve multiple files and return them as a zip file.
py/core/providers/file/postgres.py