Ejemplo n.º 1
0
 public static void CreateZip(string path, string sourceFileFolder, string comment)
 {
     if (!string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(sourceFileFolder))
     {
         SharpCompress.Common.CompressionInfo ci = new SharpCompress.Common.CompressionInfo();
         ci.DeflateCompressionLevel = SharpCompress.Compressor.Deflate.CompressionLevel.BestCompression;
         using (FileStream sw = new FileStream(path, FileMode.Create, FileAccess.Write))
         {
             SharpCompress.Writer.Zip.ZipWriter zw = new SharpCompress.Writer.Zip.ZipWriter(sw,
                                                                                            ci, comment);
             DirectoryInfo dir = new DirectoryInfo(sourceFileFolder);
             foreach (FileInfo f in dir.GetFiles("*.*", SearchOption.AllDirectories))
             {
                 using (FileStream fs = f.OpenRead())
                 {
                     string relativePath = string.Empty;
                     if (f.FullName.Length > sourceFileFolder.Length)
                     {
                         relativePath = f.FullName.Substring(sourceFileFolder.Length + 1);
                     }
                     zw.Write(relativePath, fs, f.LastWriteTime);
                 }
             }
         }
     }
 }
        private void Deploy_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog diag = new SaveFileDialog();

            diag.Title      = AMLResources.Properties.Resources.Title;
            diag.Filter     = AMLResources.Properties.Resources.SupportedCompressedFiles + DataStrings.SupportedCompressedFilesFilter;
            diag.FileName   = Configuration.Title.Replace(":", string.Empty).Replace(";", string.Empty).Replace("\\", string.Empty).Replace(" ", string.Empty).Replace("-", string.Empty) + ".zip";
            diag.DefaultExt = ".zip";
            if (diag.ShowDialog() == true)
            {
                FileHelper.DeleteFile(diag.FileName);


                SharpCompress.Common.CompressionInfo ci = new SharpCompress.Common.CompressionInfo();
                ci.DeflateCompressionLevel = SharpCompress.Compressor.Deflate.CompressionLevel.BestCompression;
                using (FileStream sw = new FileStream(diag.FileName, FileMode.Create, FileAccess.Write))
                {
                    //using (

                    SharpCompress.Writer.Zip.ZipWriter zw = new SharpCompress.Writer.Zip.ZipWriter(sw,
                                                                                                   ci,
                                                                                                   string.Format("Artemis Spaceship Bridge Simulator Mod: {0}\r\n\r\nMod ID: {1}\r\n\r\nPackaged by Artemis Mod Loader\r\n{2}\r\n\r\nDownload the latest Artemis Mod Loader from {3}",
                                                                                                                 Configuration.Title, Configuration.ID,
                                                                                                                 DateTime.Now.ToString(CultureInfo.CurrentCulture),
                                                                                                                 DataStrings.AMLUpdateURL));

                    //)
                    //{
                    DirectoryInfo dir = new DirectoryInfo(Configuration.InstalledPath);
                    foreach (FileInfo f in dir.GetFiles("*.*", SearchOption.AllDirectories))
                    {
                        using (FileStream fs = f.OpenRead())
                        {
                            string relativePath = string.Empty;
                            if (f.FullName.Length > Configuration.InstalledPath.Length)
                            {
                                relativePath = f.FullName.Substring(Configuration.InstalledPath.Length + 1);
                                //relativePath = relativePath.Substring(0, relativePath.Length - f.Name.Length);
                            }
                            zw.Write(relativePath, fs, f.LastWriteTime);
                        }
                    }
                    string           tmpPath = Path.Combine(Path.GetTempPath(), Configuration.Title.Replace(":", string.Empty).Replace(";", string.Empty).Replace("\\", string.Empty) + ".aml");
                    ModConfiguration config  = new ModConfiguration();
                    config.CopyProperties(Configuration);
                    config.InstalledPath = null;
                    config.Sequence      = 0;
                    config.ActiveFiles   = null;
                    if (config.BaseFiles.Files.Count == 0)
                    {
                        config.BaseFiles = null;
                    }
                    if (config.SubMods.SubMods.Count == 0)
                    {
                        config.SubMods = null;
                    }
                    config.Save(tmpPath);
                    using (FileStream fs = new FileStream(tmpPath, FileMode.Open, FileAccess.Read))
                    {
                        FileInfo f = new FileInfo(tmpPath);
                        zw.Write(f.Name, fs, DateTime.Now);
                    }
                    //}
                }
                Locations.MessageBoxShow("Package saved.", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }