Skip to content

Instantly share code, notes, and snippets.

@dmarcosl
Last active December 12, 2023 03:02
Show Gist options
  • Save dmarcosl/e57768283c9b81978987fa366b37d63c to your computer and use it in GitHub Desktop.
Save dmarcosl/e57768283c9b81978987fa366b37d63c to your computer and use it in GitHub Desktop.
Download several files from Firebase Storage through an Angular8 application in zip format
// dependencies
// angular 8
// "firebase": "^7.6.1",
// "file-saver": "^2.0.2",
// "jszip": "^3.2.2"
export class FileDownloadEntity {
name: string;
fileName: string;
extension: string;
constructor(name: string, fileName: string, extension: string) {
this.name = name;
this.fileName = fileName;
this.extension = extension;
}
}
import {Injectable} from '@angular/core';
import {AngularFireStorage} from '@angular/fire/storage';
import {HttpClient} from '@angular/common/http';
import {saveAs} from 'file-saver/dist/FileSaver';
import {FileDownloadEntity} from 'file-download.entity';
import * as JSZip from 'jszip';
@Injectable()
export class StorageService {
constructor(private angularFireStorage: AngularFireStorage,
private httpClient: HttpClient) {
}
public downloadZippedFiles(files: FileDownloadEntity[], zipName: string): void {
const zipFile: JSZip = new JSZip();
let count = 0;
files.forEach(file => {
this.angularFireStorage.ref('/' + file.name).getDownloadURL().subscribe(url => {
this.httpClient.get(url, {responseType: 'blob'}).subscribe(response => {
zipFile.file(file.fileName + file.extension, response, {binary: true});
count++;
if (count === files.length) {
zipFile.generateAsync({type: 'blob'}).then(content => {
saveAs(content, zipName + '.zip');
});
}
});
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment