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';
|
2018-04-26 09:59:00 +02:00
|
|
|
import * as JSZip from 'jszip';
|
2017-11-08 14:48:34 +01:00
|
|
|
import { File } from '@ionic-native/file';
|
2018-04-26 09:59:00 +02:00
|
|
|
import { CoreTextUtilsProvider } from '@providers/utils/text';
|
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 {
|
|
|
|
|
2018-04-26 09:59:00 +02:00
|
|
|
constructor(private file: File, private textUtils: CoreTextUtilsProvider) {
|
2017-11-08 14:48:34 +01:00
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2018-04-26 09:59:00 +02:00
|
|
|
/**
|
|
|
|
* Create a directory. It creates all the foldes in dirPath 1 by 1 to prevent errors.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param destination Destination parent folder.
|
|
|
|
* @param dirPath Relative path to the folder.
|
|
|
|
* @return Promise resolved when done.
|
2018-04-26 09:59:00 +02:00
|
|
|
*/
|
|
|
|
protected createDir(destination: string, dirPath: string): Promise<void> {
|
|
|
|
// Create all the folders 1 by 1 in order, otherwise it fails.
|
|
|
|
const folders = dirPath.split('/');
|
|
|
|
let promise = Promise.resolve();
|
|
|
|
|
|
|
|
for (let i = 0; i < folders.length; i++) {
|
|
|
|
const folder = folders[i];
|
|
|
|
|
|
|
|
promise = promise.then(() => {
|
|
|
|
return this.file.createDir(destination, folder, true).then(() => {
|
|
|
|
// Folder created, add it to the destination path.
|
|
|
|
destination = this.textUtils.concatenatePaths(destination, folder);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
2017-11-08 14:48:34 +01:00
|
|
|
/**
|
|
|
|
* Extracts files from a ZIP archive.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param source Path to the source ZIP file.
|
|
|
|
* @param destination Destination folder.
|
|
|
|
* @param onProgress Optional callback to be called on progress update
|
|
|
|
* @return Promise that resolves with a number. 0 is success, -1 is error.
|
2017-11-08 14:48:34 +01:00
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
unzip(source: string, destination: string, onProgress?: Function): Promise<number> {
|
2018-04-26 09:59:00 +02:00
|
|
|
|
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('/')),
|
2018-04-26 09:59:00 +02:00
|
|
|
sourceName = source.substr(source.lastIndexOf('/') + 1),
|
|
|
|
zip = new JSZip();
|
2017-11-08 14:48:34 +01:00
|
|
|
|
2018-04-26 09:59:00 +02:00
|
|
|
// Read the file first.
|
2017-11-08 14:48:34 +01:00
|
|
|
return this.file.readAsArrayBuffer(sourceDir, sourceName).then((data) => {
|
|
|
|
|
2018-04-26 09:59:00 +02:00
|
|
|
// Now load the file using the JSZip library.
|
|
|
|
return zip.loadAsync(data);
|
|
|
|
}).then((): any => {
|
|
|
|
|
|
|
|
if (!zip.files || !Object.keys(zip.files).length) {
|
2017-11-08 14:48:34 +01:00
|
|
|
// Nothing to extract.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-26 09:59:00 +02:00
|
|
|
// First of all, create the directory where the files will be unzipped.
|
|
|
|
const destParent = destination.substring(0, source.lastIndexOf('/')),
|
|
|
|
destFolderName = destination.substr(source.lastIndexOf('/') + 1);
|
|
|
|
|
|
|
|
return this.file.createDir(destParent, destFolderName, false);
|
|
|
|
}).then(() => {
|
|
|
|
|
|
|
|
const promises = [],
|
|
|
|
total = Object.keys(zip.files).length;
|
|
|
|
let loaded = 0;
|
|
|
|
|
|
|
|
for (const name in zip.files) {
|
|
|
|
const file = zip.files[name];
|
|
|
|
let promise;
|
2017-11-08 14:48:34 +01:00
|
|
|
|
|
|
|
if (!file.dir) {
|
2018-04-26 09:59:00 +02:00
|
|
|
// It's a file.
|
|
|
|
const fileDir = name.substring(0, name.lastIndexOf('/')),
|
|
|
|
fileName = name.substr(name.lastIndexOf('/') + 1),
|
|
|
|
filePromises = [];
|
|
|
|
let fileData;
|
|
|
|
|
|
|
|
if (fileDir) {
|
|
|
|
// The file is in a subfolder, create it first.
|
|
|
|
filePromises.push(this.createDir(destination, fileDir));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the file contents as a Blob.
|
|
|
|
filePromises.push(file.async('blob').then((data) => {
|
|
|
|
fileData = data;
|
|
|
|
}));
|
|
|
|
|
|
|
|
promise = Promise.all(filePromises).then(() => {
|
|
|
|
// File read and parent folder created, now write the file.
|
|
|
|
const parentFolder = this.textUtils.concatenatePaths(destination, fileDir);
|
|
|
|
|
|
|
|
return this.file.writeFile(parentFolder, fileName, fileData, {replace: true});
|
|
|
|
});
|
2017-11-08 14:48:34 +01:00
|
|
|
} else {
|
|
|
|
// It's a folder, create it if it doesn't exist.
|
2018-04-26 09:59:00 +02:00
|
|
|
promise = this.createDir(destination, name);
|
2017-11-08 14:48:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-04-26 09:59:00 +02:00
|
|
|
}
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|