Ajouter des fichiers dans un zip existant

Je peux extraire avec succès des fichiers d’un dossier zip dans un dossier, mais je ne sais pas trop comment prendre ces fichiers et les append à un fichier zip existant. Je les extrait dans un répertoire sur le bureau appelé “mod”, puis je dois les append à un autre fichier zip. Aidez-moi? Voici mon code d’extraction-

ZipFile zip = ZipFile.Read(myZip); zip.ExtractAll(outputDirectory,ExtractExistingFileAction.OverwriteSilently); 

L’aide est appréciée, merci.

Essayez, après avoir extrait les fichiers du fichier source, vous devez lire le fichier zip de destination dans un object ZipFile Vous pouvez ensuite utiliser la méthode AddFiles pour append vos fichiers source au fichier de destination, puis enregistrez-le.

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Ionic.Zip; namespace ConsoleApplication1 { class Program { static void Main(ssortingng[] args) { ssortingng myZip = @"C:\temp\test.zip"; ssortingng myOtherZip = @"C:\temp\anotherZip.zip"; ssortingng outputDirectory = @"C:\ZipTest"; using (ZipFile zip = ZipFile.Read(myZip)) { zip.ExtractAll(outputDirectory, ExtractExistingFileAction.OverwriteSilently); } using (ZipFile zipDest = ZipFile.Read(myOtherZip)) { //zipDest.AddFiles(System.IO.Directory.EnumerateFiles(outputDirectory)); //This will add them as a directory zipDest.AddFiles((System.IO.Directory.EnumerateFiles(outputDirectory)),false,""); //This will add the files to the root zipDest.Save(); } } } } 

Méthode modifiée pour append un répertoire au fichier ZipFile ( cela fonctionnera pour un seul niveau de sous-répertoire )

 using (ZipFile zipDest = ZipFile.Read(myOtherZip)) { foreach (var dir in System.IO.Directory.GetDirectories(outputDirectory)) { zipDest.AddFiles((System.IO.Directory.EnumerateFiles(dir)),false,outputDirectory ); //directory to the root } zipDest.AddFiles((System.IO.Directory.EnumerateFiles(outputDirectory)),false,""); //This will add the files to the root zipDest.Save(); } 

Méthode de suppression de fichiers d’un répertoire zip

 List files = zipDest.EntryFileNames.ToList(); // Get List of all the archives files for (int i = 0; i < files.Count; i++) { if(files[i].Contains("ZipTest")) //replace the directory you wish to delete files from here zipDest.RemoveEntry(files[i]); } 

Essayez de créer un nouveau zip:

 using (ZipFile zip = new ZipFile()) { zip.AddFile("1.txt"); zip.AddFile("favicon.png"); zip.AddFile("ElecsortingcityMagnetism.pdf"); zip.Save("BlahBlah.zip"); } 

Pour append des fichiers à un zip, essayez:

 ssortingng[] filePaths = new Ssortingng[] { ... } // Your file paths foreach (ssortingng path in filePaths) zip.AddFile(path); zip.Save("..."); // ZIP path 

7-Zip a un exécutable en ligne de commande qu’il peut utiliser.

 public static void AddFileToZip(ssortingng zipPath, ssortingng filePath) { if (!File.Exists(zipPath)) throw new FileNotFoundException("Zip was not found.", zipPath); if (!File.Exists(filePath)) throw new FileNotFoundException("File was not found.", filePath); // Create the commandline arguments. ssortingng argument = "a -tzip \"" + zipPath + "\" \"" + zipPath + "\""; // Run the 7-Zip command line executable with the commandline arguments. System.Diagnostics.Process process = System.Diagnostics.Process.Start("7za.exe", argument); process.WaitForExit(); } 

Voir: https://www.dotnetperls.com/7-zip-examples

Fonction récursive de tous les répertoires sur le chemin avec ionic.zip, C #

 static private void CompressDirRecursive(ssortingng path, ref Ionic.Zip.ZipFile dzip) { dzip.AddFiles((System.IO.Directory.GetFiles(path)), false, path); // ROOT foreach (ssortingng dir in System.IO.Directory.GetDirectories(path)) { CompressDirRecursive(dir, ref dzip);// NEXT DIRECTORY } } static private void MyTestFunction() { Ionic.Zip.ZipFile dzip = new Ionic.Zip.ZipFile(); System.IO.MemoryStream msOut = new System.IO.MemoryStream(); CompressDirRecursive("ruta que quieras", ref dzip); try { dzip.Save(outputStream: msOut); } catch (Exception ex) { throw ex; } dzip.Dispose(); // etc }