0

Hello I am trying to extract a zip like Unpack a zip using ZipInputStream (eg for Unseekable input streams). With the help of SharpZipLib. But this always give me an error:

Error:

Exception thrown: 'System.IO.DirectoryNotFoundException' in mscorlib.dll Error: Could not find a part of the path 'C:\Users\username\Documents\Visual Studio 2015\Projects\WpfApplication1\WpfApplication1\bin\Debug\ASPNETWebAPISamples-master\'.

I have even tried **ZipFile.ExtractToDirectory** build in extractor and http://dotnetzip.codeplex.com/ . they both also gave Path too Long exception .

I have found several question regarding path too long exception. But none had worked for me.

How to resolve this error? Thanks.

public static async Task HttpGetForLargeFileInRightWay()
    {
        using (HttpClient client = new HttpClient())
        {
            const string url = "https://github.com/tugberkugurlu/ASPNETWebAPISamples/archive/master.zip";
            using (HttpResponseMessage response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead))
            using (Stream streamToReadFrom = await response.Content.ReadAsStreamAsync())
            {
                try
                {
                    Debug.Print("A");
                    UnzipFromStream(streamToReadFrom, Environment.CurrentDirectory);
                    Debug.Print("M");
                }
                catch (Exception ex)
                {

                    Debug.Print("Error: " + ex.Message);
                }
            }
        }
    }


   public static void UnzipFromStream(Stream zipStream, string outFolder)
    {

        ZipInputStream zipInputStream = new ZipInputStream(zipStream);
        ZipEntry zipEntry = zipInputStream.GetNextEntry();
        Debug.Print("B");
        while (zipEntry != null)
        {
            String entryFileName = zipEntry.Name;
            // to remove the folder from the entry:- entryFileName = Path.GetFileName(entryFileName);
            // Optionally match entrynames against a selection list here to skip as desired.
            // The unpacked length is available in the zipEntry.Size property.

            byte[] buffer = new byte[4096];     // 4K is optimum

            Debug.Print("C");
            // Manipulate the output filename here as desired.
            String fullZipToPath = Path.Combine(outFolder, entryFileName);
            Debug.Print("D");
            string directoryName = Path.GetDirectoryName(fullZipToPath);
            Debug.Print("E");
            if (directoryName.Length > 0)
            {

                Debug.Print("F");
                Directory.CreateDirectory(directoryName);
                Debug.Print("G");
            }

            Debug.Print("H");
            // Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size
            // of the file, but does not waste memory.
            // The "using" will close the stream even if an exception occurs.
            using (FileStream streamWriter = File.Create(fullZipToPath))
            {
                Debug.Print("I");
                StreamUtils.Copy(zipInputStream, streamWriter, buffer);
                Debug.Print("J");
            }
            Debug.Print("K");
            zipEntry = zipInputStream.GetNextEntry();
            Debug.Print("L");
        }
    }
djkpA
  • 1,224
  • 2
  • 27
  • 57
  • Does the directory exist? – TheLethalCoder Mar 15 '17 at 11:02
  • Yeah ` Directory.CreateDirectory(directoryName)` line in code actually creates if one doesn't exist right – djkpA Mar 15 '17 at 11:03
  • Where does the error occur? And side note `fullZipToPath` seems to be a file path and `outFolder` is the directory it resides in. Why are you creating the directory on `Path.GetDirectoryName(fullZipToPath)` instead of just `outFolder`? – TheLethalCoder Mar 15 '17 at 11:06
  • Currently I haven't modified code of `SharpZipLib` . I am just using what they gave in there repo (https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples#unpack-a-zip-using-zipinputstream-eg-for-unseekable-input-streams) – djkpA Mar 15 '17 at 11:11
  • If there is any error in that please suggest it out. Thanks – djkpA Mar 15 '17 at 11:12
  • Where does the error occur? – TheLethalCoder Mar 15 '17 at 11:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/138101/discussion-between-djkp-and-thelethalcoder). – djkpA Mar 15 '17 at 11:16

1 Answers1

1

The problem is that zipInputStream.GetNextEntry() returns both directories and files within the zip file. This isn't a problem in itself but your code only handles files. To fix this you need to detect if the fullZipToPath variable holds a path to a file or a directory.

The way to do that is by inspecting the ZipEntry.IsDirectory property. Change your code to:

if (!zipEntry.IsDirectory)
{
    using (FileStream streamWriter = File.Create(fullZipToPath))
    {
        StreamUtils.Copy(zipInputStream, streamWriter, buffer);
    }
}

And the zip file is downloaded and extracted fine.

Regarding the PathTooLongException see this question for more information.

Community
  • 1
  • 1
TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69