Exception : Cannot extract for password protected zip file

Nov 12, 2009 at 7:24 AM

I am trying to unzip password protected set of files in a folder , but i am getting "Exception:Cannot Extract"

args[0]  - folder give in command line argument , how exactly find what is the cause for this error message.

  foreach (FileInfo fle in File_List)
            {
                
                try
                {
                    ZipFile zipfle = new ZipFile(fle.Name);
                    
                    foreach (ZipEntry e in zipfle)
                    {
                        e.ExtractWithPassword(args[0], "password");
                     }
                }
                catch (ZipException ex)
                {
                    Console.WriteLine("Exception Occured:{0}\n", ex.Message);
                }                
            }

Coordinator
Nov 12, 2009 at 7:36 AM

If you print out the exception, rather than just the message, you'll get some better insight.  The exception you got wraps another exception, which provides more information as to the root cause of the problem.  One of the most common problems is that the file to be extracted, already exists in the filesystem.  If you want to overwrite an existing file you need to use an ovevrload of ExtractWithPassword that allows you to explicitly specify that.

foreach (FileInfo fle in File_List)
{
    try
    {
        ZipFile zipfle = new ZipFile(fle.Name);

        foreach (ZipEntry e in zipfle)
        {
            e.ExtractWithPassword(args[0], ExtractExistingFileAction.OverwriteSilently, "password");
        }
    }
    catch (ZipException ex)
    {
        Console.WriteLine("Exception Occured:{0}\n", ex.ToString());
    }                
}


 

 

Nov 13, 2009 at 11:58 AM

 

Changed the code
Console.WriteLine("Exception Occured:{0}\n", ex.InnerException);

Here is the Error

Exception Occured:System.DllNotFoundException: Unable to load DLL 'coredll.dll':

The specified module could not be found. (Exception from HRESULT: 0x8007007E)

at Ionic.Zip.NetCfFile.CreateFileCE(String lpFileName, UInt32 dwDesiredAccess

, UInt32 dwShareMode, Int32 lpSecurityAttributes, UInt32 dwCreationDisposition,

UInt32 dwFlagsAndAttributes, Int32 hTemplateFile)

at Ionic.Zip.NetCfFile.SetTimes(String filename, DateTime ctime, DateTime ati

me, DateTime mtime)

at Ionic.Zip.ZipEntry._SetTimes(String fileOrDirectory, Boolean isFile)

at Ionic.Zip.ZipEntry.InternalExtract(String baseDir, Stream outstream, Strin

g password)

 

Exception Occured:System.DllNotFoundException: Unable to load DLL 'coredll.dll':
ecified module could not be found. (Exception from HRESULT: 0x8007007E)
   at Ionic.Zip.NetCfFile.CreateFileCE(String lpFileName, UInt32 dwDesiredAccess
, UInt32 dwShareMode, Int32 lpSecurityAttributes, UInt32 dwCreationDisposition,
UInt32 dwFlagsAndAttributes, Int32 hTemplateFile)
   at Ionic.Zip.NetCfFile.SetTimes(String filename, DateTime ctime, DateTime ati
me, DateTime mtime)
   at Ionic.Zip.ZipEntry._SetTimes(String fileOrDirectory, Boolean isFile)
   at Ionic.Zip.ZipEntry.InternalExtract(String baseDir, Stream outstream, Strin
g password)

Nov 13, 2009 at 12:03 PM

I removed reference to 

Ionic.Zip.CF.dll 

then changed to 

Ionic.Zip.dll

now i am able to unzip , but not all file still getting following error

 

 

Exception Occured:Ionic.Zip.ZipException: Cannot extract ---> System.ArgumentExc
eption: Illegal characters in path.
   at System.Security.Permissions.FileIOPermission.HasIllegalCharacters(String[]
 str)
   at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionA
ccess access, AccessControlActions control, String[] pathListOrig, Boolean check
ForDuplicates, Boolean needFullPath, Boolean copyPathList)
   at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess
access, AccessControlActions control, String[] pathList, Boolean checkForDuplica
tes, Boolean needFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, I
nt32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions o
ptions, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolea
n useLongPath)
   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, Strin
g password)
   --- End of inner exception stack trace ---
   at Ionic.Zip.ZipEntry.InternalExtract(String baseDir, Stream outstream, Strin
g password)
   at chkziblib.Program.Main(String[] args) in d:\code\vcs10\chkziblib\chkziblib
\Program.cs:line 41

 

 

Nov 13, 2009 at 12:06 PM

Errors mainly with following file name 

test_exe.php_all=12312.zip

 

?!

Coordinator
Nov 13, 2009 at 12:12 PM

First, Ionic.Zip.CF.dll should be used for .NET Compact Framework applications.  Ionic.Zip.dll should be used for applications that use the regular .NET Framework.

Second - the exception I See is "Illegal characters in path."

Do you see that?  I believe the = is not a legal character for a pathname.

 

Nov 13, 2009 at 1:23 PM

as you said "test_exe.php_all=12312.zip"  has invalid "=" sign , but tested against winrar that gives error and still able to uncompress the file.

Coordinator
Nov 13, 2009 at 2:54 PM

> tested against winrar that gives error and still able to uncompress the file.

Are you saying that WinRar was able to read and uncompress the file?

If so, that's interesting.  What is the name of the resulting extracted file in the filesystem?

---

If you don't mind changing the name, you can rename the entry prior to extraction.   ZipEntry.FileName is a read/write property.   Set it prior to calling ExtractWithPassword, and DotNetZip will use the new name to create the file.

Another alternative is to call ZipEntry.OpenReader() and  extract the contents into a file with a name of your choice.

Good luck!

 

Nov 13, 2009 at 6:38 PM

Cheeso , thanks for wonderful help.

 

winrar  creates a folder with following name "test_exe.php_all=12312" , while it does give error during uncompression. But with 7Zip no Error message & unzips fine.

 

I would try to use the ZipEntry.FileName , but i am got 1k files to uncompress.

 

 

Coordinator
Nov 13, 2009 at 6:42 PM

1k files is not that many?  anyway it is a 2 line check inside the extraction loop.  Like this:

foreach (FileInfo fle in File_List)
{
    try
    {
        ZipFile zipfle = new ZipFile(fle.Name);

        foreach (ZipEntry e in zipfle)
        {
            // check for illegal characters
            if (e.FileName.Contains("=")) 
                e.FileName = e.FileName.Replace("=","--");
            e.ExtractWithPassword(args[0], ExtractExistingFileAction.OverwriteSilently, "password");
        }
    }
    catch (ZipException ex)
    {
        Console.WriteLine("Exception Occured:{0}\n", ex.ToString());
    }                
}


Nov 13, 2009 at 6:49 PM

code worked fine & my issue solved , thanks again Cheeso , i just started using dotnetzip & its wonderful contribution.