1

I have created a wrapper (class library) for COM around the DotNetZip library that I am using in a VB6 application. I created a sample application using the wrapper on an XP machine and it works fine. When I create an installer and install the application on a Windows Server 2008 R2, it is not able to read the zip files from UNC path.

Following is my C# code:

if (File.Exists(zipFileName))
{
    // check if the directory exists
    if (Directory.Exists(extractionPath))
    {
        // remove the directory
        Directory.Delete(extractionPath, true);
    }

    // recreate the directory
    Directory.CreateDirectory(extractionPath);

    // unzip the files
    using (ZipFile loZip = ZipFile.Read(zipFileName))
    {
        // Add this line to fix an issue with the DLL
        // http://dotnetzip.codeplex.com/workitem/14087
        loZip.ParallelDeflateThreshold = -1;

        loZip.ExtractAll(extractionPath, ExtractExistingFileAction.OverwriteSilently);
    }
}
else
{
    lsResult = "@@@Error - Zip file does not exist. ";
}

The Zip file is located at the following location (this is a local folder that is shared so I am using the UNC path but this could very will be located at a network location)

    Zip File Name = '\\\\DEV-2012X\\1454444717051\\MB4.zip'

This is the exception message:

    Could not find file '\\DEV-2012X\1454444717051\MB4.zip'.

and this is the stack trace

    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
    at Ionic.Zip.ZipFile.get_ReadStream()
    at Ionic.Zip.ZipFile.ReadIntoInstance(ZipFile zf)
    at Ionic.Zip.ZipFile.Read(String fileName, TextWriter statusMessageWriter, Encoding encoding, EventHandler`1 readProgress)
    at Ionic.Zip.ZipFile.Read(String fileName)
    at ZipUtility.Zip.UnZipAllContents(String zipFileName, String extractionPath)

I can access the folder from that machine without any issues. I also tried using .net Impersonation at this link but that did not help either.

Thanks for looking into this

[EDIT - 1]

This is when i try to pass a stream to the Read method:

MemoryStream ms = new MemoryStream();

try
{
    using (FileStream file = new FileStream(zipFileName, FileMode.Open, FileAccess.Read))
    {
        byte[] bytes = new byte[file.Length];
        file.Read(bytes, 0, (int)file.Length);
        ms.Write(bytes, 0, (int)file.Length);
    }
}
catch (Exception ex)
{
    lsResult = "Stream reading crashed. " + ex.Message + Environment.NewLine + Environment.NewLine + ex.InnerException + Environment.NewLine + Environment.NewLine + ex.StackTrace + Environment.NewLine + Environment.NewLine;
}

// unzip the files
using (ZipFile loZip = ZipFile.Read(ms))
{
    // Add this line to fix an issue with the DLL
    // http://dotnetzip.codeplex.com/workitem/14087
    loZip.ParallelDeflateThreshold = -1;

    loZip.ExtractAll(extractionPath, ExtractExistingFileAction.OverwriteSilently);
}

Following is the error that I get:

    Stream reading crashed. Could not find file '\\DEV-2012X\SLWatch\Test Everything1454444717051\MB4.zip'.

And the stack trace is:

    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
    at ZipUtility.Zip.UnZipAllContents(String zipFileName, String extractionPath)

[/EDIT - 1]

[EDIT - 2]

Following are the signatures for ZipFile.Read method:

public static ZipFile Read(string fileName)
public static ZipFile Read(System.IO.Stream zipStream)
public static ZipFile Read(string fileName, ReadOptions options)
public static ZipFile Read(System.IO.Stream zipStream, ReadOptions options)

[/EDIT - 2]

[EDIT - 3]

The code as it is running now:

public string UnZipAllContents(string zipFileName, string extractionPath)
{
    string lsResult = string.Empty;
    string lsRemoteComputerName = string.Empty;
    string lsStep = "1-";

    try
    {
        lsRemoteComputerName = Path.GetPathRoot(zipFileName);
        lsStep += "2," + lsRemoteComputerName + "-";

        lsStep += "3-";
        using (UNCAccessWithCredentials unc = new UNCAccessWithCredentials())
        {
            lsStep += "3.1-";
            bool lb = unc.NetUseWithCredentials(Path.GetPathRoot(zipFileName),
                                          "Bhatti",
                                          string.Empty,
                                          "MyPassword");

            lsStep += "4, " + lb.ToString() + "-";

            string[] dirs = Directory.GetFiles(Path.GetPathRoot(zipFileName), "*",SearchOption.AllDirectories);
            lsStep += "4.1,";
            foreach (string d in dirs)
            {
                lsStep += d + ",";
            }
            lsStep += "-";

            if (File.Exists(zipFileName))
            {
                lsStep += "5-";
                // check if the directory exists
                if (Directory.Exists(extractionPath))
                {
                    // remove the directory
                    Directory.Delete(extractionPath, true);
                }

                // recreate the directory
                Directory.CreateDirectory(extractionPath);

                lsStep += "6-";
                // unzip the files
                using (ZipFile loZip = ZipFile.Read(zipFileName))
                {
                    lsStep += "7-";
                    loZip.ExtractAll(extractionPath, ExtractExistingFileAction.OverwriteSilently);
                }
                lsStep += "8-";
            }
            else
            {
                lsResult += "@@@Error - Zip file does not exist. " + lsStep;
            }
        }
    }
    catch (Exception loException)
    {
        lsResult += lsStep + Environment.NewLine + "@@@Error - " + loException.Message + Environment.NewLine + Environment.NewLine + loException.InnerException + Environment.NewLine + Environment.NewLine + loException.StackTrace;
    }

    return lsResult;
}

And this is the log that is created:

    1-2,\\DEV-2012X\SLWatch-3-3.1-4, True-4.1,\\DEV-2012X\SLWatch\BW123.zip,\\DEV-2012X\SLWatch\1454444717051.xml,\\DEV-2012X\SLWatch\1454444717051\MB4.zip,-5-6-

    @@@Error - Could not find file '\\DEV-2012X\SLWatch\1454444717051\MB4.zip'.

    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
    at Ionic.Zip.ZipFile.get_ReadStream()
    at Ionic.Zip.ZipFile.ReadIntoInstance(ZipFile zf)
    at Ionic.Zip.ZipFile.Read(String fileName, TextWriter statusMessageWriter, Encoding encoding, EventHandler`1 readProgress)
    at Ionic.Zip.ZipFile.Read(String fileName)
    at ZipUtility.Zip.UnZipAllContents(String zipFileName, String extractionPath)

[/EDIT - 3]

Bhatti
  • 23
  • 1
  • 8

1 Answers1

1

Your problem is with the file access and the exception is proof of that

because you try to load a file in to the memorystream you catch the FileNotFoundException but your code continues to run so you get another error from DotNetZip from your empty MemoryStream.

Your code should be like this:

MemoryStream ms = new MemoryStream();
bool success;
try
{    
    if(File.Exists(zipFileName))
    {
        using (FileStream file = new FileStream(zipFileName, FileMode.Open, FileAccess.Read))
        {
            byte[] bytes = new byte[file.Length];
            file.Read(bytes, 0, (int)file.Length);
            ms.Write(bytes, 0, (int)file.Length);
        }


        // unzip the files
        using (ZipFile loZip = ZipFile.Read(ms))
        {
            // Add this line to fix an issue with the DLL
            // http://dotnetzip.codeplex.com/workitem/14087
            loZip.ParallelDeflateThreshold = -1;

            loZip.ExtractAll(extractionPath, ExtractExistingFileAction.OverwriteSilently);
        }
      success = true;
    }
    else
    {
      lsResult = "ZipFile Not Found";
      success = false;
    }
 }
catch (Exception ex)
{
      success = false;
    lsResult = "Stream reading crashed. " + ex.Message + Environment.NewLine + Environment.NewLine + ex.InnerException + Environment.NewLine +     Environment.NewLine + ex.StackTrace + Environment.NewLine + Environment.NewLine;
}

[Edit] Check this to.

Check if directory exists on Network Drive [/Edit]

Community
  • 1
  • 1
Miguel de Sousa
  • 324
  • 2
  • 15
  • I am using the path as "\\DEV-2012X\1454444717051\MB4.zip" and DEV-2012X is the server that I am testing this on. Yes I know that the code should be like that but I just added it quickly to test and updated only the exception that I get from the first catch. The UNC paths are a nightmare – Bhatti Feb 24 '16 at 19:32
  • See this link and check if you add credentials it will work http://stackoverflow.com/questions/10214596/using-unc-path-with-credentials – Miguel de Sousa Feb 24 '16 at 21:13
  • tried this as well but no luck .. i can see the file there through the code but the ZipFile.Read function still crashes with the same error – Bhatti Feb 25 '16 at 16:45
  • Update your code here as it is running now. I don't think the file is reachable within your code. Can you try with the same code to a known valid path? – Miguel de Sousa Feb 25 '16 at 17:41
  • I added the current code and exception message in "Edit -3" – Bhatti Feb 25 '16 at 18:04