2

My application is a Universal Window Platform application. I try to implement a Runtime Component to decompress a zipped folder. One of my requirements is that paths which are longer than 260 characters can be handled.

public static IAsyncActionWithProgress < string > Unzip(string zipPath, string destination) {
    return AsyncInfo.Run < string > (async(_, progress) => {
        var zipFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(zipPath));

        try {
            Stream stream = await zipFile.OpenStreamForReadAsync();
            ZipArchive archive = new ZipArchive(stream);
            archive.ExtractToDirectory(destination);
        } catch (Exception e) {
            Debug.WriteLine(e.Message);
        }
    });
}

I tried to execute my method getting following exception message:

System.IO.FileNotFoundException: Could not load file or assembly 'System.IO.Compression, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
   at Zip.Zip.<>c__DisplayClass0_0.<<Unzip>b__0>d.MoveNext()
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine)
   at Zip.Zip.<>c__DisplayClass0_0.<Unzip>b__0(CancellationToken _, IProgress`1 progress)
   at System.Thread (~4340)

I tried to add System.IO.Compression with NuGet but I still get the same error. The zip file and the destination folder exist.

I tried to debug my project on Visual Studio 2015 instead of Visual Studio 2017 and found out that I can use System.IO.Compression in that way but there is a restriction for the length of paths.

Durzan
  • 53
  • 1
  • 6

1 Answers1

0

Firstly, by testing on my side, with the newest Visual Studio 2017, your code snippet could work well with file path less than 260 characters by default. If using a longer path than 260 characters visual studio 2017 will throw exception

System.IO.PathTooLongException: 'The filename or extension is too long.

which is same as Visual Studio 2015. SO please check if you are using the namespace correctly. using System.IO.Compression;

One of my requirements is that paths which are longer than 260 characters can be handled.

Secondly, the error is thrown when you tried to get the file by GetFileFromApplicationUriAsync method. So this is not the System.IO.Compression namespace issue, what you actually need to resolve is Maximum Path Length Limitation, details please reference this article. For more details about file path too long issue please try to reference this thread. But they may not suit for UWP app.

In UWP app you can use a FileOpenPicker to get the file and pass the file to the component for reading the file stream. The file picker will convert the long file name to a shot one like "xxx-UN~1.ZIP" for reading.

FileOpenPicker openpicker = new FileOpenPicker();
openpicker.FileTypeFilter.Add(".zip");
var zipfile = await openpicker.PickSingleFileAsync();
Stream stream = await zipfile.OpenStreamForReadAsync();
ZipArchive archive = new ZipArchive(stream);
archive.ExtractToDirectory(destination);

I recommend you to avoid using long file path.

Community
  • 1
  • 1
Sunteen Wu
  • 10,509
  • 1
  • 10
  • 21