locked
Compress Folder/Files (.zip)

    Question

  • Is there anyway we can compress and decompress a folder, and the files inside, and save it as .zip only using htm5/Javascript code?

    I want something like this: function createzip(FolderToBeCompressed, DestinePath) 

    anyone?

    Monday, May 14, 2012 11:12 PM

Answers

  • I had to do the same thing and ended up using a JS zip library instead:

    https://github.com/gildas-lormeau/zip.js

    Even then you need to have quite a large amount of code to read/write files. Since all IO methods in WinJS are asynchronous and you have no way of reading entire folders you have to read/write all files one by one, *all* asynchronously. Unfortunately I can't share my code.

    It is definitely possible but very tedious. I probably wrote 200 lines of code just to compress/extract a zip file to/from a folder.

    Wednesday, May 16, 2012 2:01 AM
  • Hi Alan,

    You can use the Windows.Storage.Compression library to build this archive.  You would need to add each file in the folder however as there is no interface to do an entire folder.

    -Jeff


    Jeff Sanders (MSFT)

    Tuesday, May 15, 2012 12:39 PM
    Moderator
  • As Jeff said, there is no interface for a folder so you would need to do each file separately. There is a higher level ZipArchive class in .Net's System.IO.Compression namespace which you could expose as Windows Runtime component (note that the documentation is for the desktop .Net configuration and not all methods listed there are available to Metro style apps).

    If you want something beyond what is provided by the system you would need to use a 3rd party compression library like Patrick suggested.

    --Rob

    Wednesday, May 16, 2012 3:40 PM
    Owner

All replies

  • Hi Alan,

    You can use the Windows.Storage.Compression library to build this archive.  You would need to add each file in the folder however as there is no interface to do an entire folder.

    -Jeff


    Jeff Sanders (MSFT)

    Tuesday, May 15, 2012 12:39 PM
    Moderator
  • Hi jpsanders,

    i already checked this library.. there's no sample code or anything that can help. Do you have any sample code?

    Tuesday, May 15, 2012 3:29 PM
  • I had to do the same thing and ended up using a JS zip library instead:

    https://github.com/gildas-lormeau/zip.js

    Even then you need to have quite a large amount of code to read/write files. Since all IO methods in WinJS are asynchronous and you have no way of reading entire folders you have to read/write all files one by one, *all* asynchronously. Unfortunately I can't share my code.

    It is definitely possible but very tedious. I probably wrote 200 lines of code just to compress/extract a zip file to/from a folder.

    Wednesday, May 16, 2012 2:01 AM
  • anyone with a code using Windows.Storage.Compression in JAVASCRIPT?

    This site doesn't have any tutorial or sample or even reference to use this thing. 

    Wednesday, May 16, 2012 3:35 AM
  • Did you look at the Compression sample?

    --Rob

    Wednesday, May 16, 2012 7:25 AM
    Owner
  • yes and it's compressing a single file and decompressing as a single file. There's no clue in that code showing a way to make that to compress multiple files into a SINGLE file (like a zip) AND more important, decompressing the single file extracting multiple files from it.

    Wednesday, May 16, 2012 3:22 PM
  • As Jeff said, there is no interface for a folder so you would need to do each file separately. There is a higher level ZipArchive class in .Net's System.IO.Compression namespace which you could expose as Windows Runtime component (note that the documentation is for the desktop .Net configuration and not all methods listed there are available to Metro style apps).

    If you want something beyond what is provided by the system you would need to use a 3rd party compression library like Patrick suggested.

    --Rob

    Wednesday, May 16, 2012 3:40 PM
    Owner
  • nevermind.. already did that
    Thursday, May 17, 2012 8:35 PM
  • Why not publish your code? Would make a nice WinRT extension library
    Friday, May 18, 2012 9:09 AM
  • i'll do that, but first i have to finish it and do more tests to make sure it's working fine
    Friday, May 18, 2012 3:15 PM
  • Darn, too bad there's no way to do a whole folder natively in Javascript!  That would have been really helpful.  As an example, I'm trying to add a button for a user to gather logs and it would be nice to have them end up with a zip file.  Instead I will just create a folder.
    Sunday, October 21, 2012 3:55 PM
  • we have a working unzip component and will release it soon on github. Might implement zipping first though.
    Sunday, October 21, 2012 6:53 PM