将文件添加到现有Zip中
我可以成功地将zip文件夹中的文件解压缩到一个文件夹中,但我不太清楚如何获取这些文件并将它们添加到现有的zip文件中。 我将它们提取到桌面上名为“mod”的目录中,然后我需要将它们添加到另一个zip文件中。 救命? 这是我的提取代码 –
ZipFile zip = ZipFile.Read(myZip); zip.ExtractAll(outputDirectory,ExtractExistingFileAction.OverwriteSilently);
感谢您的帮助,谢谢。
尝试尝试一下,一旦从源zip文件中提取文件,您需要将目标zip文件读入ZipFile
对象,然后可以使用AddFiles
方法将源文件添加到目标文件,然后保存。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Ionic.Zip; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string myZip = @"C:temptest.zip"; string myOtherZip = @"C:tempanotherZip.zip"; string 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(); } } } }
将目录添加到ZipFile的修改方法( 这将适用于单个子目录级别 )
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(); }
从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]); }
尝试创建一个新的zip:
using (ZipFile zip = new ZipFile()) { zip.AddFile("1.txt"); zip.AddFile("favicon.png"); zip.AddFile("ElectricityMagnetism.pdf"); zip.Save("BlahBlah.zip"); }
要将文件添加到zip,请尝试:
string[] filePaths = new String[] { ... } // Your file paths foreach (string path in filePaths) zip.AddFile(path); zip.Save("..."); // ZIP path
7-Zip有一个可以使用的命令行可执行文件。
public static void AddFileToZip(string zipPath, string 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. string 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(); }
请参阅: https : //www.dotnetperls.com/7-zip-examples
使用ionic.zip,C#递归函数路径上的所有目录
上述就是C#学习教程:将文件添加到现有Zip中分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
static private void CompressDirRecursive(string path, ref Ionic.Zip.ZipFile dzip) { dzip.AddFiles((System.IO.Directory.GetFiles(path)), false, path); // ROOT foreach (string 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 }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1028182.html