public static void BuildPackage(IEnumerable<string> includes, string outputFileName) { File.Delete(outputFileName); using (var zipFile = new ZipFile(outputFileName)) { foreach (var include in includes) { if (include.Length < 3) { throw new PackManException("Include option must have following format: f|d|p:<value>."); } char type = char.ToLower(include[0]); string value = include.Substring(2); switch (type) { case 'f': zipFile.AddFile(value); break; case 'd': zipFile.AddDirectory(value); break; case 'p': zipFile.AddSelectedFiles(value,true); break; } } zipFile.Save(); } }
public virtual void Export(Site site, System.IO.Stream outputStream) { ExportLabels(site); using (ZipFile zipFile = new ZipFile()) { zipFile.AddSelectedFiles("*.*", new Label(site).PhysicalPath, ""); zipFile.Save(outputStream); } }
private void BackupFiles() { EMMServer server = Manager.Server; string backupFile = Path.Combine(server.Settings.BackupRoot, string.Format("backup-{0:yyyyMMdd-HHmmss}.zip", DateTime.Now)); using (ZipFile zip = new ZipFile()) { zip.AddSelectedFiles("*.txt", server.Settings.MinecraftRoot, @"minecraft"); zip.AddSelectedFiles("*.jar", server.Settings.MinecraftRoot, @"minecraft"); zip.AddSelectedFiles("*.properties", server.Settings.MinecraftRoot, @"minecraft"); zip.AddDirectory(mWorldPath, @"minecraft\" + Path.GetFileName(mWorldPath)); try { zip.Save(backupFile); } catch (Exception e) { server.RaiseServerMessage(string.Format("ERROR: Unable to save backup! {0}", e.Message)); } } }
public void Export(Site site, IEnumerable<Label> labels, IEnumerable<string> categories, Stream outputStream) { if ((labels == null || labels.Count() == 0) && (categories == null || categories.Count() == 0)) { this.ExportLabelsToDisk(site); using (ZipFile zipFile = new ZipFile()) { zipFile.AddSelectedFiles("*.json", new LabelPath(site).PhysicalPath, ""); zipFile.Save(outputStream); } } else { var tempFolder = GetImportExportTempFolder(site); Kooboo.IO.IOUtility.DeleteDirectory(tempFolder, true); if (labels != null) { foreach (var item in labels) { var label = _rawLabelProvider.Get(item); if (label != null) { var storage = GetStorage(GetImportExportLabelFile(site, label.Category)); storage.Add(label); } } } if (categories != null) { foreach (var item in categories) { foreach (var label in _rawLabelProvider.GetLabels(site, item).ToArray()) { var storage = GetStorage(GetImportExportLabelFile(site, label.Category)); storage.Add(label); } } } using (ZipFile zipFile = new ZipFile()) { zipFile.AddSelectedFiles("*.json", tempFolder, ""); zipFile.Save(outputStream); } Kooboo.IO.IOUtility.DeleteDirectory(tempFolder, true); } }
/// <summary> /// Compress the binaries into ZIP file /// </summary> /// <param name="path"> /// The path. /// </param> /// <param name="outFile"> /// The out File. /// </param> private void CompressBinariesDir(string path, string outFile) { var dir = Path.GetDirectoryName(outFile); Directory.CreateDirectory(dir); if (File.Exists(outFile)) { File.Delete(outFile); } string inDir = Path.GetFullPath(path); using (var zip = new ZipFile()) { zip.CompressionLevel = CompressionLevel.BestCompression; zip.AddSelectedFiles("name != *.pdb", inDir, string.Empty, true); zip.Save(outFile); } }
public void ShellApplication_SelectedFiles_Unzip() { string zipFileToCreate = Path.Combine(TopLevelDir, "ShellApplication_SelectedFiles_Unzip.zip"); TestContext.WriteLine("ZipFile version: {0}", ZipFile.LibraryVersion); // create and fill the directories string extractDir = "extract"; string dirToZip = "files"; TestContext.WriteLine("creating dir '{0}' with files", dirToZip); Directory.CreateDirectory(dirToZip); int numFilesToAdd = _rnd.Next(5) + 6; int numFilesAdded = 0; int baseSize = _rnd.Next(0x100ff) + 8000; int nFilesInSubfolders = 0; Dictionary<string, byte[]> checksums = new Dictionary<string, byte[]>(); var flist = new List<string>(); for (int i = 0; i < numFilesToAdd && nFilesInSubfolders < 2; i++) { string fileName = string.Format("Test{0}.txt", i); if (i != 0) { int x = _rnd.Next(4); if (x != 0) { string folderName = string.Format("folder{0}", x); fileName = Path.Combine(folderName, fileName); if (!Directory.Exists(Path.Combine(dirToZip, folderName))) Directory.CreateDirectory(Path.Combine(dirToZip, folderName)); nFilesInSubfolders++; } } fileName = Path.Combine(dirToZip, fileName); TestUtilities.CreateAndFillFileBinary(fileName, baseSize + _rnd.Next(28000)); var key = Path.GetFileName(fileName); var chk = TestUtilities.ComputeChecksum(fileName); checksums.Add(key, chk); flist.Add(fileName); numFilesAdded++; } // Create the zip archive var sw = new System.IO.StringWriter(); using (ZipFile zip1 = new ZipFile()) { zip1.StatusMessageTextWriter = sw; //zip1.StatusMessageTextWriter = Console.Out; zip1.AddSelectedFiles("*.*", dirToZip, "", true); zip1.Save(zipFileToCreate); } TestContext.WriteLine(sw.ToString()); // Verify the number of files in the zip Assert.AreEqual<int>(TestUtilities.CountEntries(zipFileToCreate), numFilesAdded, "Incorrect number of entries in the zip file."); // run the unzip script string script = GetScript("VbsUnzip-ShellApp.vbs"); this.Exec(cscriptExe, String.Format("\"{0}\" {1} {2}", script, zipFileToCreate, Path.Combine(TopLevelDir, extractDir))); // check the files in the extract dir foreach (var fqPath in flist) { var f = Path.GetFileName(fqPath); var extractedFile = fqPath.Replace("files", "extract"); Assert.IsTrue(File.Exists(extractedFile), "File does not exist ({0})", extractedFile); var chk = TestUtilities.ComputeChecksum(extractedFile); Assert.AreEqual<String>(TestUtilities.CheckSumToString(checksums[f]), TestUtilities.CheckSumToString(chk), String.Format("Checksums for file {0} do not match.", f)); checksums.Remove(f); } Assert.AreEqual<Int32>(0, checksums.Count, "Not all of the expected files were found in the extract directory."); }
public void Selector_AddSelectedFiles_Checkcase_directory_2() { string zipFileToCreate = "AddSelectedFiles_Checkcase.zip"; string shortDirToZip = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()).ToUpper(); string dirToZip = Path.Combine(TopLevelDir, shortDirToZip); // fully qualified var files = TestUtilities.GenerateFilesFlat(shortDirToZip); string keyword = "Ammon"; int n = _rnd.Next(3)+2; for (int i=0; i < n; i++) { Directory.SetCurrentDirectory(dirToZip); string subdir = keyword + i; TestUtilities.GenerateFilesFlat(subdir); Directory.SetCurrentDirectory(subdir); var f2 = Directory.GetFiles(".", "*.*"); int k = 2; Array.ForEach(f2, x => { File.Move(x, String.Format("{0}.{1:D5}.txt", keyword.ToUpper(), k++)); }); } Directory.SetCurrentDirectory(TopLevelDir); TestContext.WriteLine("Create zip file"); using (ZipFile zip1 = new ZipFile()) { var criterion = "name = *\\" + keyword + "?\\*.txt"; zip1.AddSelectedFiles(criterion, ".\\" + shortDirToZip, "files", true); zip1.Save(zipFileToCreate); } TestContext.WriteLine("Verify case of entry FileNames"); int nEntries = 0; // now, verify that we have not downcased entry.FileName using (ZipFile zip2 = new ZipFile(zipFileToCreate)) { foreach (var entry in zip2.Entries) { TestContext.WriteLine("Check {0}", entry.FileName); Assert.AreNotEqual<String>(entry.FileName, entry.FileName.ToLower(), entry.FileName); nEntries++; } } Assert.IsFalse(nEntries < 3, "not enough entries"); }
public void Selector_AddSelectedFiles_Checkcase_directory() { string zipFileToCreate = "AddSelectedFiles_Checkcase.zip"; string dirToZip = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()).ToUpper(); var files = TestUtilities.GenerateFilesFlat(dirToZip); var txtFiles = Directory.GetFiles(dirToZip, "*.txt", SearchOption.AllDirectories); Assert.IsFalse(txtFiles.Length < 3, "not enough entries (n={0})", txtFiles.Length); using (ZipFile zip1 = new ZipFile()) { zip1.AddSelectedFiles("*.txt", dirToZip); zip1.Save(zipFileToCreate); } int nEntries = 0; // now, verify that we have not downcased the filenames using (ZipFile zip2 = new ZipFile(zipFileToCreate)) { foreach (var entry in zip2.Entries) { Assert.IsFalse(entry.FileName.Equals(entry.FileName.ToLower())); nEntries++; } } Assert.IsFalse(nEntries < 3, "not enough entries (n={0})", nEntries); }
public void Selector_AddSelectedFiles_Checkcase_file() { string zipFileToCreate = "AddSelectedFiles_Checkcase.zip"; string dirToZip = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()); var files = TestUtilities.GenerateFilesFlat(dirToZip); Directory.SetCurrentDirectory(dirToZip); var f2 = Directory.GetFiles(".", "*.*"); Array.ForEach(f2, x => { File.Move(x,Path.GetFileName(x).ToUpper()); }); Directory.SetCurrentDirectory(TopLevelDir); var txtFiles = Directory.GetFiles(dirToZip, "*.txt", SearchOption.AllDirectories); using (ZipFile zip1 = new ZipFile()) { zip1.AddSelectedFiles("*.txt", dirToZip); zip1.Save(zipFileToCreate); } int nEntries = 0; // now, verify that we have not downcased the filenames using (ZipFile zip2 = new ZipFile(zipFileToCreate)) { foreach (var entry in zip2.Entries) { Assert.IsFalse(entry.FileName.Equals(entry.FileName.ToLower())); nEntries++; } } Assert.IsFalse(nEntries < 2, "not enough entries"); }
public void Selector_AddSelectedFiles_2() { string zipFileToCreate = "Selector_AddSelectedFiles_2.zip"; string dirToZip = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()); var files = TestUtilities.GenerateFilesFlat(dirToZip); var txtFiles = Directory.GetFiles(dirToZip, "*.txt", SearchOption.AllDirectories); using (ZipFile zip1 = new ZipFile()) { zip1.AddSelectedFiles("*.txt"); zip1.Save(zipFileToCreate); } Assert.AreEqual<Int32>(0, TestUtilities.CountEntries(zipFileToCreate)); using (ZipFile zip1 = new ZipFile()) { zip1.AddSelectedFiles("*.txt", true); zip1.Save(zipFileToCreate); } Assert.AreEqual<Int32>(txtFiles.Length, TestUtilities.CountEntries(zipFileToCreate)); using (ZipFile zip1 = new ZipFile()) { zip1.AddSelectedFiles("*.txt", ".", true); zip1.Save(zipFileToCreate); } Assert.AreEqual<Int32>(txtFiles.Length, TestUtilities.CountEntries(zipFileToCreate)); }
public void Selector_AddSelectedFiles() { Directory.SetCurrentDirectory(TopLevelDir); Trial[] trials = new Trial[] { new Trial { Label = "name", C1 = "name = *.txt", C2 = "name = *.bin" }, new Trial { Label = "name (shorthand)", C1 = "*.txt", C2 = "*.bin" }, new Trial { Label = "attributes (Hidden)", C1 = "attributes = H", C2 = "attributes != H" }, new Trial { Label = "attributes (ReadOnly)", C1 = "attributes = R", C2 = "attributes != R" }, new Trial { Label = "mtime", C1 = "mtime < 2007-01-01", C2 = "mtime > 2007-01-01" }, new Trial { Label = "atime", C1 = "atime < 2007-01-01", C2 = "atime > 2007-01-01" }, new Trial { Label = "ctime", C1 = "ctime < 2007-01-01", C2 = "ctime > 2007-01-01" }, new Trial { Label = "size", C1 = "size < 7500", C2 = "size >= 7500" }, new Trial { Label = "name & size", C1 = "name = *.bin AND size > 7500", C2 = "name != *.bin OR size <= 7500", }, new Trial { Label = "name, size & attributes", C1 = "name = *.bin AND size > 8kb and attributes = H", C2 = "name != *.bin OR size <= 8kb or attributes != H", }, new Trial { Label = "name, size, time & attributes.", C1 = "name = *.bin AND size > 7k and mtime < 2007-01-01 and attributes = H", C2 = "name != *.bin OR size <= 7k or mtime > 2007-01-01 or attributes != H", }, }; _txrx = TestUtilities.StartProgressMonitor("AddSelectedFiles", "AddSelectedFiles", "starting up..."); string[] zipFileToCreate = { Path.Combine(TopLevelDir, "Selector_AddSelectedFiles-1.zip"), Path.Combine(TopLevelDir, "Selector_AddSelectedFiles-2.zip") }; Assert.IsFalse(File.Exists(zipFileToCreate[0]), "The zip file '{0}' already exists.", zipFileToCreate[0]); Assert.IsFalse(File.Exists(zipFileToCreate[1]), "The zip file '{0}' already exists.", zipFileToCreate[1]); int count1, count2; SetupFiles(); var topLevelFiles = Directory.GetFiles(fodderDirectory, "*.*", SearchOption.TopDirectoryOnly); string currentDir = Directory.GetCurrentDirectory(); _txrx.Send(String.Format("pb 0 max {0}", 2 * (trials.Length + 1))); _txrx.Send("pb 0 step"); for (int m = 0; m < trials.Length; m++) { _txrx.Send("test AddSelectedFiles"); _txrx.Send("pb 1 max 4"); _txrx.Send(String.Format("status test {0}/{1}: creating zip #1/2", m + 1, trials.Length)); TestContext.WriteLine("==============================================="); TestContext.WriteLine("AddSelectedFiles() [{0}]", trials[m].Label); using (ZipFile zip1 = new ZipFile()) { zip1.AddSelectedFiles(trials[m].C1, fodderDirectory, ""); zip1.Save(zipFileToCreate[0]); } count1 = TestUtilities.CountEntries(zipFileToCreate[0]); TestContext.WriteLine("C1({0}) Count({1})", trials[m].C1, count1); _txrx.Send("pb 1 step"); System.Threading.Thread.Sleep(100); _txrx.Send("pb 0 step"); _txrx.Send(String.Format("status test {0}/{1}: creating zip #2/2", m + 1, trials.Length)); using (ZipFile zip1 = new ZipFile()) { zip1.AddSelectedFiles(trials[m].C2, fodderDirectory, ""); zip1.Save(zipFileToCreate[1]); } count2 = TestUtilities.CountEntries(zipFileToCreate[1]); TestContext.WriteLine("C2({0}) Count({1})", trials[m].C2, count2); Assert.AreEqual<Int32>(topLevelFiles.Length, count1 + count2); _txrx.Send("pb 1 step"); /// ======================================================= /// Now, select entries from that ZIP _txrx.Send(String.Format("status test {0}/{1}: selecting zip #1/2", m + 1, trials.Length)); using (ZipFile zip1 = ZipFile.Read(zipFileToCreate[0])) { var selected1 = zip1.SelectEntries(trials[m].C1); Assert.AreEqual<Int32>(selected1.Count, count1); } _txrx.Send("pb 1 step"); _txrx.Send(String.Format("status test {0}/{1}: selecting zip #2/2", m + 1, trials.Length)); using (ZipFile zip1 = ZipFile.Read(zipFileToCreate[1])) { var selected2 = zip1.SelectEntries(trials[m].C2); Assert.AreEqual<Int32>(selected2.Count, count2); } _txrx.Send("pb 1 step"); _txrx.Send("pb 0 step"); } }
public void Selector_Twiddle_wi10153() { // workitem 10153: // // When calling AddSelectedFiles(String,String,String,bool), and when the // actual filesystem path uses mixed case, but the specified directoryOnDisk // argument is downcased, AND when the filename contains a ~ (weird, I // know), verify that the path replacement works as advertised, and entries // are rooted in the directoryInArchive specified path. string zipFileToCreate = Path.Combine(TopLevelDir, "Selector_Twiddle.zip"); string dirToZip = "dirToZip"; var keyword = "Gamma"; var files = TestUtilities.GenerateFilesFlat(dirToZip); int k = 0; Directory.SetCurrentDirectory(dirToZip); Array.ForEach(files, x => { File.Move(Path.GetFileName(x), String.Format("~{0}.{1:D5}.txt", keyword, k++)); }); Directory.SetCurrentDirectory(TopLevelDir); using (ZipFile zip = new ZipFile()) { // must use ToLower to force case mismatch zip.AddSelectedFiles("name != *.zip*", dirToZip.ToLower(), "", true); zip.Save(zipFileToCreate); } int nEntries = 0; using (ZipFile zip = ZipFile.Read(zipFileToCreate)) { foreach (var e in zip) TestContext.WriteLine("entry {0}", e.FileName); TestContext.WriteLine(""); foreach (var e in zip) { TestContext.WriteLine("check {0}", e.FileName); Assert.IsFalse(e.FileName.Contains("/"), "The filename contains a path, but shouldn't"); nEntries++; } } Assert.IsTrue(nEntries>2, "Not enough entries"); }
public void Selector_SelectFiles_DirName_wi9176() { // workitem 9176 //Directory.SetCurrentDirectory(TopLevelDir); _txrx= TestUtilities.StartProgressMonitor("SelectFiles-DirName", "Select Files by DirName", "workitem 9176"); SetupFiles(); var binFiles = Directory.GetFiles(fodderDirectory, "*.bin", SearchOption.AllDirectories); int[] eCount = new int[2]; _txrx.Send("pb 0 max 2"); for (int i = 0; i < 2; i++) { string zipFileToCreate = Path.Combine(TopLevelDir, String.Format("Selector_SelectFiles_DirName_wi9176-{0}.zip", i)); _txrx.Send("pb 1 max 4"); _txrx.Send("pb 1 value 0"); string d = fodderDirectory; if (i == 1) d += "\\"; TestContext.WriteLine("==============================================="); TestContext.WriteLine("AddSelectedFiles(cycle={0})", i); using (ZipFile zip1 = new ZipFile()) { zip1.AddSelectedFiles("name = *.bin", d, "", true); _txrx.Send("pb 1 step"); zip1.Save(zipFileToCreate); } _txrx.Send("pb 1 step"); Assert.AreEqual<Int32>(TestUtilities.CountEntries(zipFileToCreate), binFiles.Length, "The Zip file has the wrong number of entries."); _txrx.Send("pb 2 step"); using (ZipFile zip1 = ZipFile.Read(zipFileToCreate)) { foreach (var e in zip1) { if (e.FileName.Contains("/")) eCount[i]++; } } _txrx.Send("pb 1 step"); if (i==1) Assert.AreEqual<Int32>(eCount[0], eCount[1], "Inconsistent results when the directory includes a path.", i); _txrx.Send("pb 0 step"); } }
public void Selector_SelectFiles_DirName_wi8245_2() { // workitem 8245 string zipFileToCreate = Path.Combine(TopLevelDir, "Selector_SelectFiles_DirName_wi8245_2.zip"); //Directory.SetCurrentDirectory(TopLevelDir); SetupFiles(); var fodderFiles = Directory.GetFiles(fodderDirectory, "*.*", SearchOption.AllDirectories); TestContext.WriteLine("==============================================="); TestContext.WriteLine("AddSelectedFiles()"); using (ZipFile zip1 = new ZipFile()) { zip1.AddSelectedFiles(fodderDirectory, null, "fodder", true); zip1.Save(zipFileToCreate); } Assert.AreEqual<Int32>(TestUtilities.CountEntries(zipFileToCreate), fodderFiles.Length, "The Zip file has the wrong number of entries."); }
private void Zipit(object sender, DoWorkEventArgs e) { int delay = 1200; // ms to keep form open after completion try { using (var zip = new ZipFile()) { zip.AddSelectedFiles(selectionCriteria, ".", "", true); zip.SaveProgress += this.SaveProgress; zip.Save(zipfileName); } } catch (Exception ex1) { this.label1.Text = "Exception: " + ex1.ToString(); delay = 4000; } var timer1 = new System.Timers.Timer(delay); timer1.Enabled = true; timer1.AutoReset = false; timer1.Elapsed += this.OnTimerEvent; }
static void Main(string[] args) { Dictionary<string, string> paths = new Dictionary<string, string>(); paths.Add("minecraft", "Minecraft"); paths.Add("cache", "Cache"); paths.Add("c10t", "C10t"); paths.Add("overviewer", "Overviewer"); paths.Add("backups", "Backups"); paths.Add("maps", "Maps"); paths.Add("biomeextractor", "BiomeExtractor"); // The base-path is the location of the current executable string buildRoot = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Substring(8)); string projectRoot = Path.GetDirectoryName(buildRoot); string deployRoot = Path.Combine(projectRoot, "Deploy"); string sourceRoot = Path.Combine(projectRoot, "Solution"); string stagingRoot = Path.Combine(deployRoot, "staging"); // Clean and create the deployment root if (Directory.Exists(deployRoot)) { Directory.Delete(deployRoot, true); } Directory.CreateDirectory(deployRoot); Directory.CreateDirectory(stagingRoot); // Create the empty directories foreach (KeyValuePair<string, string> p in paths) { if (!Directory.Exists(p.Value)) { Directory.CreateDirectory(Path.Combine(stagingRoot, p.Value)); } } // Copy the main deployment files CopyFiles(buildRoot, stagingRoot, "Server.exe"); CopyFiles(buildRoot, stagingRoot, "LibNbt.dll"); CopyFiles(buildRoot, stagingRoot, "Ionic.Zip.Reduced.dll"); // Copy the default scheduler config CopyFiles(Path.Combine(sourceRoot, "Server", "Engine", "Scheduler"), stagingRoot, "sample.scheduler.xml"); // Build the monolithic license readme string readme = File.ReadAllText(Path.Combine(projectRoot, "readme.txt")); string nbtLic = File.ReadAllText(Path.Combine(sourceRoot, "LibNbt", "libnbt.txt")); string ionLic = File.ReadAllText(Path.Combine(sourceRoot, "Server", "Engine", "Ionic.txt")); StringBuilder finalReadme = new StringBuilder(); finalReadme.AppendLine("##### Contents"); finalReadme.AppendLine("##1## Readme"); finalReadme.AppendLine("##2## LibNbt License"); finalReadme.AppendLine("##3## Ionic Zip License"); finalReadme.AppendLine("#####"); finalReadme.AppendLine(""); finalReadme.AppendLine(""); finalReadme.AppendLine("################################################################################"); finalReadme.AppendLine("##1## Readme"); finalReadme.AppendLine("################################################################################"); finalReadme.AppendLine(File.ReadAllText(Path.Combine(projectRoot, "readme.txt"))); finalReadme.AppendLine(""); finalReadme.AppendLine(""); finalReadme.AppendLine("################################################################################"); finalReadme.AppendLine("##2## LibNbt License"); finalReadme.AppendLine("################################################################################"); finalReadme.AppendLine(File.ReadAllText(Path.Combine(sourceRoot, "LibNbt", "libnbt.txt"))); finalReadme.AppendLine(""); finalReadme.AppendLine(""); finalReadme.AppendLine("################################################################################"); finalReadme.AppendLine("##3## Ionic Zip License"); finalReadme.AppendLine("################################################################################"); finalReadme.AppendLine(File.ReadAllText(Path.Combine(sourceRoot, "Server", "Engine", "Ionic.txt"))); using (StreamWriter file = new System.IO.StreamWriter(Path.Combine(stagingRoot, "readme.txt"))) { file.WriteLine(finalReadme.ToString()); } // zip everything up using (ZipFile zip = new ZipFile()) { zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression; zip.AddSelectedFiles("*", stagingRoot, "", true); zip.Save(Path.Combine(deployRoot, "Enigma-MM.zip")); } }