/// <summary>Creates a new packaged archive</summary> /// <param name="currentPackage">The package data we wish to compress into an archive</param> /// <param name="compressionType">The compression type to use for this archive</param> /// <param name="packageFile">The filename to save the package as</param> /// <param name="packageImage">The path to the image for this package, if applicable</param> /// <param name="packageFiles">The list of files to save within the package</param> public static void CreatePackage(Package currentPackage, CompressionType compressionType, string packageFile, string packageImage, List <PackageFile> packageFiles) { int cf = 0; string fp = String.Empty; try { using (var zip = File.OpenWrite(packageFile)) { SharpCompress.Common.ArchiveType type; SharpCompress.Common.CompressionType compression; switch (compressionType) { case CompressionType.Zip: type = ArchiveType.Zip; compression = SharpCompress.Common.CompressionType.LZMA; break; case CompressionType.BZ2: type = ArchiveType.Zip; compression = SharpCompress.Common.CompressionType.BZip2; break; case CompressionType.TarGZ: type = ArchiveType.Tar; compression = SharpCompress.Common.CompressionType.GZip; break; default: type = ArchiveType.Zip; compression = SharpCompress.Common.CompressionType.LZMA; break; } using (var zipWriter = WriterFactory.Open(zip, type, compression)) { for (int fileToAdd = 0; fileToAdd < packageFiles.Count; fileToAdd++) { cf = fileToAdd; PackageFile currentFile = packageFiles[fileToAdd]; fp = currentFile.absolutePath; if (currentFile.absolutePath.EndsWith("thumbs.db", StringComparison.InvariantCultureIgnoreCase)) { //Skip thumbs.db files, as they're often locked when creating or extracting //Pointless too..... continue; } if (new FileInfo(currentFile.absolutePath).Length == 0) { //Don't archive zero-byte files, as Sharpcompress doesn't like them..... continue; } //Add file to archive zipWriter.Write(currentFile.relativePath, currentFile.absolutePath); OnProgressChanged(null, new ProgressReport((int)((double)fileToAdd / packageFiles.Count * 100), currentFile.absolutePath)); } //Create temp directory and XML file var tempXML = System.IO.Path.GetTempPath() + System.IO.Path.GetRandomFileName() + "package.xml"; string tempPath = System.IO.Path.GetDirectoryName(tempXML); if (tempPath == null) { throw new Exception("Unable to create the temporary directory for package compression."); } Directory.CreateDirectory(tempPath); using (StreamWriter sw = new StreamWriter(tempXML)) { //TODO: Let's see if we can get the serializer working everywhere in the solution..... //Haven't checked whether these are read by the reader yet. XmlSerializer listWriter = new XmlSerializer(typeof(SerializedPackage)); listWriter.Serialize(sw, new SerializedPackage { Package = currentPackage }); } //Write out XML zipWriter.Write("Package.xml", tempXML); //Write out image if (System.IO.File.Exists(packageImage)) { zipWriter.Write("Package.png", packageImage); } } } } catch (Exception ex) { OnProblemReport(null, new ProblemReport((int)((double)cf / packageFiles.Count * 100), fp, ex)); } }
//This uses a folder picker dialog to add folders private void addPackageItemsButton_Click(object sender, EventArgs e) { bool DialogOK = false; string[] files = null; string folder = String.Empty; string folderDisplay = String.Empty; if (OpenTK.Configuration.RunningOnMacOS || OpenTK.Configuration.RunningOnLinux) { //Mono doesn't like our fancy folder selector //Some versions of OS-X crash, and Linux just falls back- Safer to specifically use the old version on these... var MonoDialog = new FolderBrowserDialog(); if (MonoDialog.ShowDialog() == DialogResult.OK) { folder = System.IO.Directory.GetParent(MonoDialog.SelectedPath).ToString(); folderDisplay = MonoDialog.SelectedPath; files = System.IO.Directory.GetFiles(folderDisplay, "*.*", System.IO.SearchOption.AllDirectories); DialogOK = true; } } else { //Use the fancy folder selector dialog on Windows var dialog = new FolderSelectDialog(); if (dialog.Show(Handle)) { DialogOK = true; folder = System.IO.Directory.GetParent(dialog.FileName).ToString(); folderDisplay = dialog.FileName; files = System.IO.Directory.GetFiles(dialog.FileName, "*.*", System.IO.SearchOption.AllDirectories); } } if (DialogOK && files.Length != 0) { filesToPackageBox.Text += folderDisplay + Environment.NewLine; var tempList = new List<PackageFile>(); for (int i = 0; i < files.Length; i++) { var File = new PackageFile { absolutePath = files[i], relativePath = files[i].Replace(folder, ""), }; tempList.Add(File); } if (filesToPackage == null) { filesToPackage = new List<PackageFile>(); } filesToPackage.AddRange(DatabaseFunctions.FindFileLocations(tempList)); } }