I am using the library ICSharpCode.SharpZipLib.Zip;

My code is follows:

  • The path is root. \\ALAWP\\THIS\\ACORD\\
  • I'm zipping them to the ZIPDirectory

However when it's done the file is not named acord_combined.txt, instead it's called ACORD\acord_combined.txt

What am I doing wrong?

public void CleanRoot()
{
    DirectoryInfo RootDi = new DirectoryInfo(FilePrep.RootDirectory);
    string ZipDirectory = FilePrep.RootDirectory + "\\processed\\AceKey"+ DateTime.Now.ToString("yyyyMMdd_H;mm;ss") +".zip";

    ZipOutputStream NewZipOutput = new ZipOutputStream(File.Create(ZipDirectory));

    foreach (FileInfo fi in RootDi.GetFiles("acord*.*"))
    {
        Compress(ref NewZipOutput, fi);
        //MoveFile(fi.FullName,ZipDirectory);
    }

    NewZipOutput.Finish();
    NewZipOutput.Close();
}

public void Compress(ref ZipOutputStream ZipFolder, FileInfo fi)
{
    try
    {
       FileStream fsFileToBeZipped = fi.OpenRead();

       ZipEntry entry = new ZipEntry(fi.FullName);
       ZipFolder.PutNextEntry(entry);
       int size = 2048;
       byte[] buffer = new byte[size];

       while (true)
       {
          size = fsFileToBeZipped.Read(buffer, 0, buffer.Length);

          if (size > 0)
             ZipFolder.Write(buffer, 0, size);
          else
             break;
       }   //end while ( true )

       fsFileToBeZipped.Close();

       //prepare and delete file
       fi.Attributes = FileAttributes.Normal;
       //fi.Delete();
   }   //end try
   catch (Exception e)
   {
        Console.WriteLine("Error zipping File.  Error - " + e.Message);
   }   //end catch
}
有帮助吗?

解决方案

Your problem is right here

new ZipEntry(fi.FullName);

The argument to zipEntry is the path in the zip file, not the full path the compressed data comes from. Usually zip libraries, such as 7zip and SharpZip, expose a way to create an "entry path" but the actual data written to the zip is from the full path.

Probably what you want is

 new ZipEntry(Path.GetFileName(fi.fullName))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top