1

I'm trying to use DotNetZip to handle zip files, but whenever I try to open a file I get the following error:

[SEVERE] System.ArgumentException: FileStream will not open Win32 devices such as disk partitions and tape drives. Avoid use of "\\.\" in the path.
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, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)
at Ionic.Zip.ZipEntry.InternalExtract(String baseDir, Stream outstream, String password)
at Ionic.Zip.ZipEntry.Extract(String baseDirectory)
at Ionic.Zip.ZipFile._InternalExtractAll(String path, Boolean overrideExtractExistingProperty)
at Ionic.Zip.ZipFile.ExtractAll(String path)
at ModsInstaller.Form1.MergeDirectories(String Path1, String Path2) in C:\Users\Admin\documents\visual studio 2010\Projects\ModsInstaller\ModsInstaller\Form1.cs:line 275
at ModsInstaller.Form1.CustomInstallForge() in C:\Users\Admin\documents\visual studio 2010\Projects\ModsInstaller\ModsInstaller\Form1.cs:line 259
at ModsInstaller.Form1.btn_install_Click(Object sender, EventArgs e) in C:\Users\Admin\documents\visual studio 2010\Projects\ModsInstaller\ModsInstaller\Form1.cs:line 120

and here's the code:

    private void MergeDirectories(string Path1, string Path2)
    {
        string outDirectory = Path.GetFullPath(workspace + "\\temp\\dir");

        if (!Directory.Exists(outDirectory))
            Directory.CreateDirectory(outDirectory);

        Path1 = Path.GetFullPath(Path1);
        Path2 = Path.GetFullPath(Path2);            

        Log("Extracting {0} to temp dir.", Path1);
        using (ZipFile zip = ZipFile.Read(Path1))
        {
            zip.ExtractAll(outDirectory); //this line throws the error
        }
        Log("Extraction sucessfull");

        Log("Extracted {0} to temp dir.", Path2);
        ZipFile.Read(Path2).ExtractAll(Path.GetFullPath(workspace + "\\temp\\dir"));
        Log("Extraction sucessfull");

        ZipFile z = new ZipFile(workspace + "\\temp\\build.jar");
        z.AddDirectory(workspace + "\\temp\\dir");
        z.Save();
        z.Dispose();
    }

and when I insert a breakpoint I see that:

outDirectory = "C:\\Users\\Admin\\documents\\visual studio 2010\\Projects\\ModsInstaller\\ModsInstaller\\bin\\Debug\\temp\\dir"

Can anyone point out what I'm doing wrong?

Thanks.

Doodles
  • 109
  • 1
  • 11

2 Answers2

3

I had the same error with CON file name. It is not because of Ionic.Zip lib., but rather due to the Windows file naming convention.

Check the content of the first ZIP file if it has some unusual file names. For example, in Windows you cannot create file with name CON, AUX, NUL, COM1, etc.

You can read more about it in reserved names section: https://learn.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#file_and_directory_names

Solution to it is to take other zip file for testing or extract it under unix system or ask file provider to send vulnerable file(s) with differentia file name or at least lower case.

Arek Bee
  • 329
  • 3
  • 9
  • True. Windows does not let you create files with these names without using third-party software: CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9 – Stefan Đorđević Oct 06 '19 at 19:25
1

Usage

MergeDirectories("Sample 1.zip", "Sample 2.zip", "Merged.zip");

Code:

    private void MergeDirectories(string filePath1, string filePath2, string mergedName)
    {
        string workspace = Environment.CurrentDirectory;
        filePath1 = Path.Combine(workspace, filePath1);
        filePath2 = Path.Combine(workspace, filePath2);
        mergedName = Path.Combine(workspace, mergedName);

        if (File.Exists(mergedName))
        {
            File.Delete(mergedName);
        }

        DirectoryInfo zip1 = OpenAndExtract(filePath1);
        DirectoryInfo zip2 = OpenAndExtract(filePath2);

        string merged = Path.GetTempFileName();
        using (ZipFile z = new ZipFile())
        {
            z.AddDirectory(zip1.FullName);
            z.AddDirectory(zip2.FullName);
            z.Save(merged);
        }

        zip1.Delete(true);
        zip2.Delete(true);

        File.Move(merged, mergedName);
    }

    private DirectoryInfo OpenAndExtract(string path)
    {
        string tmpName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName());
        string tmp = Path.Combine(Path.GetTempPath(), tmpName);

        FileInfo sourcePath = new FileInfo(path);
        DirectoryInfo tempPath = Directory.CreateDirectory(tmp);

        using (ZipFile zip = ZipFile.Read(sourcePath.FullName))
        {
            zip.ExtractAll(tempPath.FullName);
        }

        return tempPath;
    }
jrbeverly
  • 1,611
  • 14
  • 20
  • With further tests I cannot seem to reproduce your error. What was the value of the `workspace` variable? – jrbeverly Jul 27 '13 at 01:38
  • huh, I copied your code to test it, but it still throws the error. The value of workspace is "C:\\Users\\Admin\\documents\\visual studio 2010\\Projects\\ModsInstaller\\ModsInstaller\\bin\\Debug" – Doodles Jul 27 '13 at 01:48
  • what is the error? Is it exactly the same error as before? or any difference? – jrbeverly Jul 27 '13 at 01:50