2013-08-07 2 views
0

내 코드에서 JSZip 객체를 만들 수 있지만 Windows 8 앱의 로컬 저장소에 저장하는 데 문제가 있습니다. 내가 찾을 수있는 예제는 브라우저의 location.href를 설정하여 다운로드를 시작합니다. 이는 실제로 나를위한 옵션이 아닙니다.JSZip을 사용하여 win8 앱의 로컬 저장소에 zip 파일을 저장하려면 어떻게해야합니까?

아래 코드를 포함 시켰습니다. 내가 끝내는 zip 파일은 유효하지 않으며 열 수 없습니다. 어떤 도움을 주시면 감사하겠습니다. 참고로

:이 라인에 뭔가를 할 수 JSZip

 function _zipTest() { 
     var dbFile = null; 
     var zipData = null; 
     Windows.Storage.StorageFile.getFileFromPathAsync(config.db.path) 
      .then(function (file) { 
       dbFile = file; 
       return Windows.Storage.FileIO.readBufferAsync(file); 
      }) 
      .then(function (buffer) { 
       //Read the database file into a byte array and create a new zip file 
       zipData = new Uint8Array(buffer.length); 
       var dataReader = Windows.Storage.Streams.DataReader.fromBuffer(buffer); 
       dataReader.readBytes(zipData); 
       dataReader.close(); 

       var localFolder = Windows.Storage.ApplicationData.current.localFolder; 
       return localFolder.createFileAsync(dbFile.displayName.concat('.zip'), Windows.Storage.CreationCollisionOption.replaceExisting) 
      }) 
      .then(function (file) { 
       //Write the zip data to the new zip file 
       var zip = new JSZip(); 
       zip.file(dbFile.displayName, zipData); 
       var content = zip.generate(); 

       return Windows.Storage.FileIO.writeTextAsync(file, content); 
      }); 
    } 

답변

1

. 이 코드는 임시 폴더에 유효한 .zip 파일을 생성하는 것 같습니다.

var zip = new JSZip(); 
    var storage = Windows.Storage; 
    storage.StorageFile.getFileFromApplicationUriAsync(new Windows.Foundation.Uri('ms-appx:///images/logo.png')).then(function ongetfile(file) 
    { 
     var blob = MSApp.createFileFromStorageFile(file); 
     var url = URL.createObjectURL(blob, { oneTimeOnly: true }); 
     return WinJS.xhr({ url: url, responseType: 'arraybuffer' }); 
    }).then(function onreadbuffer(req) 
    { 
     var b = req.response; 
     zip.file('logo.png', b); 
     return storage.ApplicationData.current.temporaryFolder.createFileAsync('a.zip', storage.CreationCollisionOption.replaceExisting); 
    }).then(function onnewfile(out) 
    { 
     var content = zip.generate({ type: 'uint8array' }); 
     return storage.FileIO.writeBytesAsync(out, content); 
    }).then(null, function onerror(error) 
    { 
     // TODO: error handling 
    }); 
관련 문제