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");
}
}
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.