我正在使用HTML / JS在Windows 8中工作。我正在尝试使用FilePicker提取一个Zip文件,这是一个已选择的文件。
要提取Zip文件,我使用此page。
在此链接中,有一个提取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);
});
}
在此之前,我将JSZIP
库添加到了Project文件夹中
帮帮我,如何将库集成到我的项目中。这是我的项目Link
修改
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;
});
}
现在正在处理错误。但它没有像提取我的文件那样做任何事情。
注意:
- 如果有人知道任何比我或其他图书馆更好的方法,我可以用它来提取WinJS文件;请建议我。
答案 0 :(得分:1)
嗯,我猜你没有创建getFileAsUint8Array
函数(或者至少,你没有在上面显示它)。我正在做类似的事情(虽然从XHR调用获取zip文件)。一旦我有了zip文件和我想放入zip文件的文件夹,我会做类似下面的代码。
但请注意,我必须修改此代码,因为我做了其他一些事情,因此我没有完全按原样进行测试(显然它在上面的代码中不起作用)。
这是(大部分)完整代码:
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
)
});
};
答案 1 :(得分:0)