Skip to content

Instantly share code, notes, and snippets.

@pke
Created October 22, 2012 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pke/3934325 to your computer and use it in GitHub Desktop.
Save pke/3934325 to your computer and use it in GitHub Desktop.
unzip component
CreationCollisionOption = Windows.Storage.CreationCollisionOption
describe 'Zip component', ->
it 'should handle OpenDocument containers', ->
spec.async ->
uri = "resource/test1.odt".toAppPackageUri()
stream = Windows.Storage.Streams.RandomAccessStreamReference.createFromUri(uri)
runtime.zip.ZipArchive.createFromStreamReferenceAsync(stream)
.then (archive) ->
expect(archive.files.length).toEqual 17
archive.getFileContentsAsync('meta.xml')
.then (buffer) ->
expect(buffer).toBeTruthy()
.then ->
archive.getFileContentsAsync('settings.xml')
.then (buffer) ->
expect(buffer).toBeTruthy()
it 'should handle Office Open XML containers', ->
spec.async ->
uri = "resource/test1.docx".toAppPackageUri()
stream = Windows.Storage.Streams.RandomAccessStreamReference.createFromUri(uri)
runtime.zip.ZipArchive.createFromStreamReferenceAsync(stream)
.then (archive) ->
expect(archive.files.length).toEqual 9
archive.getFileContentsAsync('docProps/core.xml')
.then (buffer) ->
expect(buffer).toBeTruthy()
it 'should extract single files to disk', ->
tempFolder = Windows.Storage.ApplicationData.current.temporaryFolder
spec.async ->
uri = "resource/test1.odt".toAppPackageUri()
stream = Windows.Storage.Streams.RandomAccessStreamReference.createFromUri(uri)
tempFolder.createFileAsync('temp_meta.xml', CreationCollisionOption.replaceExisting)
.then (destinationFile) ->
runtime.zip.ZipArchive.createFromStreamReferenceAsync(stream)
.then (archive) ->
# meta.xml is stored uncompressed
archive.extractFileAsync('meta.xml', destinationFile)
.then ->
destinationFile.getBasicPropertiesAsync()
.then (fileProperties) ->
expect(fileProperties.size).toBeGreaterThan 0
tempFolder.createFileAsync('temp_content.xml', CreationCollisionOption.replaceExisting)
.then (destinationFile) ->
runtime.zip.ZipArchive.createFromStreamReferenceAsync(stream)
.then (archive) ->
# content.xml is stored deflated
archive.extractFileAsync('content.xml', destinationFile)
.then ->
destinationFile.getBasicPropertiesAsync()
.then (fileProperties) ->
expect(fileProperties.size).toBeGreaterThan 0
it 'should extract whole archives to disk', ->
tempFolder = Windows.Storage.ApplicationData.current.temporaryFolder
spec.async ->
tempFolder.createFolderAsync('unzipped', CreationCollisionOption.replaceExisting)
.then (folder) ->
uri = "resource/test1.odt".toAppPackageUri()
stream = Windows.Storage.Streams.RandomAccessStreamReference.createFromUri(uri)
runtime.zip.ZipArchive.createFromStreamReferenceAsync(stream)
.then (archive) ->
archive.extractAllAsync(folder)
.then ->
queryOptions = new Windows.Storage.Search.QueryOptions()
queryOptions.folderDepth = Windows.Storage.Search.FolderDepth.deep
fileQueryResults = folder.createFileQueryWithOptions(queryOptions)
fileQueryResults.getFilesAsync()
.then (extractedFiles) ->
expect(extractedFiles.length).toBeGreaterThan 0
it 'should throw invalid argument exception for non-existing files', ->
tempFolder = Windows.Storage.ApplicationData.current.temporaryFolder
spec.async ->
uri = "resource/test1.odt".toAppPackageUri()
stream = Windows.Storage.Streams.RandomAccessStreamReference.createFromUri(uri)
tempFolder.createFileAsync('temp.xml', CreationCollisionOption.replaceExisting)
.then (destinationFile) ->
runtime.zip.ZipArchive.createFromStreamReferenceAsync(stream)
.then (archive) ->
archive.extractFileAsync('foobar.dat', destinationFile)
.then ->
jasmine.getEnv().currentSpec.fail("This should have failed")
, (error) ->
expect(error.number).toEqual -2147024809
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment