2017-11-08 14:48:34 +01:00
|
|
|
// (C) Copyright 2015 Martin Dougiamas
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Zip } from '@ionic-native/zip';
|
|
|
|
import { JSZip } from 'jszip';
|
|
|
|
import { File } from '@ionic-native/file';
|
2018-03-01 16:55:49 +01:00
|
|
|
import { CoreMimetypeUtilsProvider } from '@providers/utils/mimetype';
|
2017-11-08 14:48:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Emulates the Cordova Zip plugin in desktop apps and in browser.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class ZipMock extends Zip {
|
|
|
|
|
|
|
|
constructor(private file: File, private mimeUtils: CoreMimetypeUtilsProvider) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts files from a ZIP archive.
|
|
|
|
*
|
|
|
|
* @param {string} source Path to the source ZIP file.
|
|
|
|
* @param {string} destination Destination folder.
|
|
|
|
* @param {Function} [onProgress] Optional callback to be called on progress update
|
|
|
|
* @return {Promise<number>} Promise that resolves with a number. 0 is success, -1 is error.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
unzip(source: string, destination: string, onProgress?: Function): Promise<number> {
|
2017-11-08 14:48:34 +01:00
|
|
|
// Replace all %20 with spaces.
|
|
|
|
source = source.replace(/%20/g, ' ');
|
|
|
|
destination = destination.replace(/%20/g, ' ');
|
|
|
|
|
2018-01-29 10:05:20 +01:00
|
|
|
const sourceDir = source.substring(0, source.lastIndexOf('/')),
|
2017-11-08 14:48:34 +01:00
|
|
|
sourceName = source.substr(source.lastIndexOf('/') + 1);
|
|
|
|
|
|
|
|
return this.file.readAsArrayBuffer(sourceDir, sourceName).then((data) => {
|
2018-01-29 10:05:20 +01:00
|
|
|
const zip = new JSZip(data),
|
2017-11-08 14:48:34 +01:00
|
|
|
promises = [],
|
|
|
|
total = Object.keys(zip.files).length;
|
2018-01-29 10:05:20 +01:00
|
|
|
let loaded = 0;
|
2017-11-08 14:48:34 +01:00
|
|
|
|
2018-01-29 10:05:20 +01:00
|
|
|
if (!zip.files || !zip.files.length) {
|
2017-11-08 14:48:34 +01:00
|
|
|
// Nothing to extract.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
zip.files.forEach((file, name) => {
|
|
|
|
let type,
|
|
|
|
promise;
|
|
|
|
|
|
|
|
if (!file.dir) {
|
|
|
|
// It's a file. Get the mimetype and write the file.
|
|
|
|
type = this.mimeUtils.getMimeType(this.mimeUtils.getFileExtension(name));
|
2018-01-29 10:05:20 +01:00
|
|
|
promise = this.file.writeFile(destination, name, new Blob([file.asArrayBuffer()], { type: type }));
|
2017-11-08 14:48:34 +01:00
|
|
|
} else {
|
|
|
|
// It's a folder, create it if it doesn't exist.
|
|
|
|
promise = this.file.createDir(destination, name, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
promises.push(promise.then(() => {
|
|
|
|
// File unzipped, call the progress.
|
|
|
|
loaded++;
|
2018-01-29 10:05:20 +01:00
|
|
|
onProgress && onProgress({ loaded: loaded, total: total });
|
2017-11-08 14:48:34 +01:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2018-01-29 10:05:20 +01:00
|
|
|
return Promise.all(promises).then(() => {
|
2017-11-08 14:48:34 +01:00
|
|
|
return 0;
|
|
|
|
});
|
2018-01-29 10:05:20 +01:00
|
|
|
}).catch(() => {
|
2017-11-08 14:48:34 +01:00
|
|
|
// Error.
|
|
|
|
return -1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|