2013-12-12 111 views
3

我正在使用HTML/JS的Windows 8中工作。我正在嘗試使用FilePicker提取一個Zip文件。使用庫提取ZIp文件JSZip

提取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圖書館項目文件夾之前。
幫助我,我如何將圖書館整合到我的項目中。這裏是我的項目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文件的其他圖書館更好;請給我建議。

回答

1

嗯,我猜你還沒有創建getFileAsUint8Array函數(或者至少,你沒有在上面顯示它)。我正在做類似的事情(儘管取而代之的是從XHR調用中獲取壓縮文件)。一旦我有了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 
       ) 
    }); 
}; 
+0

不幸的是,我不明白你的代碼。你可以添加更多的描述性或任何來源鏈接將是有益的。 是的,我正在miising'getFileAsUint8Array'函數。添加功能後,我沒有得出結論。 謝謝 – Kumar

+0

沒有源代碼鏈接...這是代碼。至於'getFileAsUnit8Array'函數...你會得到一個不同的錯誤?你的問題仍然說你得到了'未定義'的錯誤......你呢?還是你看到別的東西? – jakerella

+0

嗨,我編輯我的問題,我忘了更改我的帖子中的描述。 &我只是休耕這個鏈接[http://blog.appliedis.com/2013/09/18/zipping-and-unzipping-files-in-a-winjs-application/] 你可以嘗試我的樣本項目我加了。我無法更改我的項目中的編輯。現在我失業了。謝謝 – Kumar