none
[UWP][JS] How can I compress an entire folder?

    Question

  • Can I use the Windows.Storage.Compression Namespace to compress an entire directory and its contents?

    I've looked at the samples, and it only demonstrates how to compress one or multiple files, but it doesn't show how to compress an entire directory while preserving the folder structure. Even if I were to compress every file in my directory using the method shown in the sample, it would be a flat structure.

    Is there anything like the .NET ZipFile.CreateFromDirectory method that I can use?

    Wednesday, November 7, 2018 5:17 PM

All replies

  • Hi,

    Well, ZipFile is a .net API and you could not use it directly. And it also needs direct file access permissions. So ZipFile API might not be suitable for your scenarios.  

    A possible way for this is that you could try to iterate the directory and use APIs from Windows.Storage.Compression namespace for each file. After that you could add them to the same archive.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 19, 2018 7:36 AM
    Moderator
  • One of the requirements is that I'm able to preserve information about the folder structure.

    Say that I have a directory like

    dir/

        file1.txt

        file2.txt

        dir2/

            file3.txt

            file4.txt

    Correct me if I'm wrong, but if I were to iterate the directory and use Windows.Storage.Compression on each file, I'd lose information about where the file is in this structure. Once I've compressed the files this way, how would I decompress them, and put them back in the sub-directories where they should go? What mechanism would know that file3.txt goes inside of dir/dir2, or even that the file is named file3.txt?

    Monday, November 19, 2018 5:50 PM
  • Hi,

    This is possible to achieve through a bit of logic. You could try to get the path of each file, combine these into categories and save it in another place. Once you need to decompress the files, check your previously saved entries and place the files into right places.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 26, 2018 6:07 AM
    Moderator