[英]Extract ZIp File Using Library JSZip
I am working in windows 8 using HTML/JS. 我正在使用HTML / JS在Windows 8中工作。 I am trying to Extract a Zip file which is picked file using FilePicker.
我试图使用FilePicker提取一个Zip文件,这是一个挑选文件。
To extract Zip file I am using this page . 要提取Zip文件,我正在使用此页面 。
In this Link there is a function to Extract Zip file unzipAsync
在此链接中,有一个提取Zip文件
unzipAsync
function unzipAsync(filePath, replaceIfExists) {
var fileCollisionOption = replaceIfExists ?
storage.CreationCollisionOption.replaceExisting :
storage.CreationCollisionOption.failIfExists;
return storage.StorageFile
.getFileFromPathAsync(filePath)
.then(getFileAsUint8Array)
.then(function (zipFileContents) {
//Create the zip data in memory
var zip = new JSZip(zipFileContents);
//Extract files
var promises = [];
var lf = storage.ApplicationData.current.localFolder;
_.each(zip.files, function (zippedFile) {
//Create new file
promises.push(lf
.createFileAsync(zippedFile.name, fileCollisionOption)
.then(function (localStorageFile) {
//Copy the zipped file's contents into the local storage file
var fileContents = zip.file(zippedFile.name).asUint8Array();
return storage.FileIO
.writeBytesAsync(localStorageFile, fileContents);
})
);
});
return WinJS.Promise.join(promises);
});
}
Before this I added JSZIP
Library to Project folder. 在此之前,我将
JSZIP
库添加到Project文件夹中。
Help me, How Can I integrate the Library to my project. 帮帮我,如何将库集成到我的项目中。 Here is my project Link
这是我的项目链接
Edit: 编辑:
function getFileAsUint8Array(file) {
return storage.FileIO.readBufferAsync(file)
.then(function (buffer) {
//Read the file into a byte array
var fileContents = new Uint8Array(buffer.length);
var dataReader = storage.Streams.DataReader.fromBuffer(buffer);
dataReader.readBytes(fileContents);
dataReader.close();
return fileContents;
});
}
Now it is working with out error. 现在它正在处理错误。 But it is not doing any thing like extracting my file.
但它没有像提取我的文件那样做任何事情。
NOTE: 注意:
- If anyone knows any another way which better than this or other Library which I can use to extract file for WinJS; - 如果有人知道任何其他比我或其他图书馆更好的方法,我可以用它来提取WinJS文件; please suggest me.
请建议我。
Well, I'm guessing you haven't created a getFileAsUint8Array
function (or at least, you aren't showing it above). 好吧,我猜你没有创建一个
getFileAsUint8Array
函数(或者至少,你没有在上面显示它)。 I am doing something similar (although getting the zip file from an XHR call instead). 我正在做类似的事情(虽然从XHR调用获取zip文件)。 Once I have the zip file and the folder I want to put the zip files in I do something like the code below.
一旦我有zip文件和我想放入zip文件的文件夹,我会做类似下面的代码。
Note, however, that I had to modify this code as I do a few other things, so I haven't tested it exactly as is (and obviously it wouldn't work within your code above). 但请注意,我必须修改此代码,因为我做了一些其他的事情,所以我没有完全按原样进行测试(显然它在上面的代码中不起作用)。
Here's the (mostly) full code: 这是(大部分)完整代码:
WinJS.xhr({ "url": zipUrl, "responseType": "arraybuffer" })
.done(
function (e) {
if (!e.getResponseHeader("content-type") === "application/zip") {
console.error("Remote file was not sent with correct Content-Type: expected 'application/zip', but received '" + e.getResponseHeader("content-type") + "'");
}
unzipAndStore(new JSZip(e.response), someLocalFolder);
},
function() { /* handle ajax errors */ }
);
/**
* @param {JSZip} jszipobj The JSZip object
* @param {StorageFolder} localFolder The folder to unzip into
* @return {Promise}
*/
var unzipAndStore = function (jszipobj, localFolder) {
var promises = [];
Object.keys(jszipobj.files).forEach(function (key) {
var fileName;
// ignore folder entries, they're handled as needed below
if (/\/$/.test(key)) { return; }
fileName = jszipobj.files[key].name.match(/[^\/]+\.[^\.\/]+$/);
if (!fileName) {
console.error("Unable to process zip entry without proper filename: ", jszipobj.files[key].name);
return;
}
fileName = fileName[0];
promises.push(
getFolderFromPathRecursive(jszipobj.files[key].name, localFolder)
.then(
function (subFolder) {
console.log("creating file in folder: ", fileName, subFolder.name);
return subFolder.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.replaceExisting)
}
)
.then(
function (localStorageFile) {
return Windows.Storage.FileIO
.writeBytesAsync(localStorageFile, jszipobj.file(jszipobj.files[key].name).asUint8Array());
}
)
);
});
return WinJS.Promise.join(promises);
};
/**
* Promise completes with the lowest level folder in the given path,
* creating subfolders along the way
* @param {String} path The path to the lowest subfolder you want a reference to
* @param {StorageFolder} rootFolder The folder to begin at for this iteration
* @return {Promise}
*/
var getFolderFromPathRecursive = function (path, rootFolder) {
var normalizedPath = path.replace(/\/?[^\/]+\.[^\.\/]+$/, ""), // remove a possible filename from the end of the path
folders = normalizedPath.split(/\//), // get an array of the folders in the path
subFolderName = folders.shift(); // remove the first folder in the path as the new one to create
return new WinJS.Promise(function (complete, error) {
if (!subFolderName || !subFolderName.length) {
complete(rootFolder);
return;
}
rootFolder
.createFolderAsync(subFolderName, Windows.Storage.CreationCollisionOption.openIfExists)
.then(
function (folder) {
return getFolderFromPathRecursive(folders.join("/"), folder);
},
error
)
.then(
function(folder) {
complete(folder);
return;
},
error
)
});
};
I did using Decompressing Sample by adding C# windows runtime to my JavaScript project 我通过在我的JavaScript项目中添加C#windows runtime来使用Decompressing Sample
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.