Pergunta

I have developed a console application which zips up files within a directory with password to a destination folder. I am zipping files within a directory to a destination folder with password.

This is the code. In my local PC its working fine. I have created setup for this application and installed on my PC as well as other PCs too. In my PC its working fine. but on other PCs its giving exception. Can you please tell me what could be the issue and how should I solve it? Is this permission related issue or some other issue.

{
    int codePage = 0;
    ZipEntry e = null;
    string entryComment = null;
    string entryDirectoryPathInArchive = "";
    string strFullFilePath = "";
    using (ZipFile zip = new ZipFile())
    {
        //zip.ExtractExistingFile= ExtractExistingFileAction.OverwriteSilently;
        // zip.ExtractAll(Environment.CurrentDirectory, .OverwriteSilently);
        //var result = zipFile.Any(entry => entry.FileName.EndsWith("input.txt"));

        zip.StatusMessageTextWriter = System.Console.Out;
        zip.UseUnicodeAsNecessary = true;
        for (int i = 0; i < args.Length; i++)
        {
            switch (args[i])
            {
                case "-p":
                    i++;
                    if (args.Length <= i) Usage();
                    zip.Password = (args[i] == "") ? null : args[i];
                    break;

                case "-flat":
                    entryDirectoryPathInArchive = "";
                    break;

                        //case "-utf8":
                        //    zip.UseUnicodeAsNecessary = true;
                        //    break;

                case "-64":
                    zip.UseZip64WhenSaving = Zip64Option.Always;
                    break;

                case "-d":
                    i++;
                    if (args.Length <= i) Usage();
                    string entryName = args[i];

                    if (!System.IO.Directory.Exists(args[i]))
                    {
                        System.IO.Directory.CreateDirectory(args[i]);

                        //System.Console.Error.WriteLine("Path ({0}) does not exist.", strFullFilePath);
                        //m_log.Error("Destination Path "+strFullFilePath+" does not exist.");
                    }

                    System.DateTime Timestamp = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
                    string strFileName = Timestamp.ToShortDateString();
                    strFullFilePath = args[i] + strFileName + ".zip";
                    break;

                case "-c":
                    i++;
                    if (args.Length <= i) Usage();
                    entryComment = args[i];  // for the next entry
                    break;

                case "-zc":
                    i++;
                    if (args.Length <= i) Usage();
                    zip.Comment = args[i];
                    break;

                case "-cp":
                    i++;
                    if (args.Length <= i) Usage();
                    System.Int32.TryParse(args[i], out codePage);
                    if (codePage != 0)
                        zip.ProvisionalAlternateEncoding = System.Text.Encoding.GetEncoding(codePage);
                    break;

                case "-s":
                    i++;
                    if (args.Length <= i) Usage();
                    zip.AddDirectory(args[i], args[i]);
                    //entryDirectoryPathInArchive = args[i];
                    break;

                default:
                    // UpdateItem will add Files or Dirs, recurses subdirectories
                    //zip.UpdateItem(args[i], entryDirectoryPathInArchive);

                    // try to add a comment if we have one
                    if (entryComment != null)
                    {
                        // can only add a comment if the thing just added was a file.
                        if (zip.EntryFileNames.Contains(args[i]))
                        {
                            e = zip[args[i]];
                            e.Comment = entryComment;
                        }
                        else
                            Console.WriteLine("Warning: ZipWithEncryption.exe: ignoring comment; cannot add a comment to a directory.");

                        // reset the comment
                        entryComment = null;
                    }
                    break;
                }
            }

            zip.Save(strFullFilePath);
        }
    }

These are the exception messages

Saving....
Exception: System.IO.DirectoryNotFoundException: Could not find a part of the pa
th.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.Move(String sourceFileName, String destFileName)
   at Ionic.Zip.ZipFile.Save()
   at Ionic.Zip.ZipFile.Save(String zipFileName)
   at Ionic.Zip.Examples.ZipWithEncryption.Main(String[] args)
Exception:
Exception: Could not find a part of the path.
Exception: mscorlib
Exception:    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullP
ath)
   at System.IO.File.Move(String sourceFileName, String destFileName)
   at Ionic.Zip.ZipFile.Save()
   at Ionic.Zip.ZipFile.Save(String zipFileName)
   at Ionic.Zip.Examples.ZipWithEncryption.Main(String[] args)
Exception: Void WinIOError(Int32, System.String)
Foi útil?

Solução

I think this code is strange :

System.DateTime Timestamp = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
string strFileName = Timestamp.ToShortDateString();
strFullFilePath = args[i] + strFileName + ".zip";

First, you can write

DateTime Timestamp = DateTime.Now;

Then ToShortDateString depend on CurrentCulture. Force what you want (for exemple :)

String strFileName = Timestamp.ToString("yyyyMMdd");

Exception is from here, on my mind. With wrong Culture, you can have "/" in your format string.

Then, use Path.Combine instead of operator + (no error with a missing slash)

strFullFilePath = Path.Combine(args[i], strFileName + ".zip");

Edit For those who can't format DateTime, here my code (and that works) :

public static class Constantes
{
    public const String DateTimePattern = "yyyyMMddHHmmss";
}

public class SaveProcessViewModel : NotificationObject
{
[...]
    String zipFileName = System.Environment.MachineName + "-" + DateTime.Now.ToString(Constantes.DateTimePattern) + ".zip";
    String tempZipFile = Path.Combine(Path.GetTempPath(), zipFileName);

    zip.Save(tempZipFile);
[...]
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top