Laden...

SharpZipLib - Extrahieren funktioniert nicht immer

Erstellt von hypersurf vor 15 Jahren Letzter Beitrag vor 15 Jahren 1.314 Views
H
hypersurf Themenstarter:in
523 Beiträge seit 2008
vor 15 Jahren
SharpZipLib - Extrahieren funktioniert nicht immer

Hallo,

ich benutze die SharpZipLib um mehrere Dateien in meiner Applikation zu packen. Funktioniert soweit auch problemlos.

Wenn ich das Archiv jetzt z. B. mit Winrar entpacken will bekomme ich Fehlermeldungen, dass die im Archiv enthaltenen Dateien korrupt seien. Dasselbe bei 7zip. Nur mit der ZIP-Funktion von Windows-XP lassen sich die Dateien problemlos extrahieren.

Ich möchte aber sicherstellen, dass sich das Archiv mit allen gängigen Programmen extrahieren lässt.

Hier mein Quellcode:


      private static void WriteZipFile(List<string> filesToZip, string path, int compression)
      {
         if (compression < 0 || compression > 9)
            throw new ArgumentException("Invalid compression rate.");

         if (!Directory.Exists(new FileInfo(path).Directory.ToString()))
            throw new ArgumentException("The Path does not exist.");

         foreach (string c in filesToZip)
            if (!File.Exists(c))
               throw new ArgumentException(string.Format("The File{0}does not exist!", c));


         Crc32 crc32 = new Crc32();
         ZipOutputStream stream = new ZipOutputStream(File.Create(path));
         stream.SetLevel(compression);

         for (int i = 0; i < filesToZip.Count; i++)
         {
            ZipEntry entry = new ZipEntry(Path.GetFileName(filesToZip[i]));
            entry.DateTime = DateTime.Now;

            using (FileStream fs = File.OpenRead(filesToZip[i]))
            {
               byte[] buffer = new byte[fs.Length];
               fs.Read(buffer, 0, buffer.Length);
               entry.Size = fs.Length;
               fs.Close();
               crc32.Reset();
               crc32.Update(buffer);
               entry.Crc = crc32.Value;
               stream.PutNextEntry(entry);
               stream.Write(buffer, 0, buffer.Length);
            }
         }
         stream.Finish();
         stream.Close();
      }

compression wird mit 0 übergeben.

Hat jemand eine Idee woran das liegen könnte. Mache ich beim Packen in meiner Methode was falsch?

H
hypersurf Themenstarter:in
523 Beiträge seit 2008
vor 15 Jahren

Problem selber gelöst:

      private static void WriteZipFile(List<string> filesToZip, string path, int compression)
      {
         if (compression < 0 || compression > 9)
            throw new ArgumentException("Invalid compression rate.");

         if (!Directory.Exists(new FileInfo(path).Directory.ToString()))
            throw new ArgumentException("The Path does not exist.");

         foreach (string c in filesToZip)
            if (!File.Exists(c))
               throw new ArgumentException(string.Format("The File{0}does not exist!", c));


         ZipOutputStream stream = new ZipOutputStream(File.Create(path));
         stream.SetLevel(compression);

         for (int i = 0; i < filesToZip.Count; i++)
         {
            ZipEntry entry = new ZipEntry(Path.GetFileName(filesToZip[i]));
            entry.DateTime = DateTime.Now;

            entry.Size = System.IO.File.Open(filesToZip[i], System.IO.FileMode.OpenOrCreate,
                         System.IO.FileAccess.Read, System.IO.FileShare.Read).Length;
            stream.PutNextEntry(entry);

            byte[] buffer = new byte[4096];
            using (FileStream fs = File.OpenRead(filesToZip[i]))
            {
               StreamUtils.Copy(fs, stream, buffer);
            }
         }
         stream.Finish();
         stream.Close();
      }