public void UpdateZipInMemory(Stream zipStream, Stream entryStream, String entryName) { // The zipStream is expected to contain the complete zipfile to be updated ZipFile zipFile = new ZipFile(zipStream); zipFile.BeginUpdate(); // To use the entryStream as a file to be added to the zip, // we need to put it into an implementation of IStaticDataSource. CustomStaticDataSource sds = new CustomStaticDataSource(); sds.SetStream(entryStream); // If an entry of the same name already exists, it will be overwritten; otherwise added. zipFile.Add(sds, entryName); // Both CommitUpdate and Close must be called. zipFile.CommitUpdate(); // Set this so that Close does not close the memorystream zipFile.IsStreamOwner = false; zipFile.Close(); // Reposition to the start for the convenience of the caller. zipStream.Position = 0; }
void SetContent(string loc, byte[] content) { ZipFile zipFile = new ZipFile(FileLoc); // Must call BeginUpdate to start, and CommitUpdate at the end. zipFile.BeginUpdate(); if (loc.Contains('/')) { int i = loc.IndexOf('/'); string dir = loc.Remove(i + 1, loc.Length - i - 1); if (zipFile.FindEntry(dir, true) < 0) { zipFile.AddDirectory(dir); } } CustomStaticDataSource sds = new CustomStaticDataSource(); using (MemoryStream ms = new MemoryStream(content)) { sds.SetStream(ms); // If an entry of the same name already exists, it will be overwritten; otherwise added. zipFile.Add(sds, loc); // Both CommitUpdate and Close must be called. zipFile.CommitUpdate(); zipFile.Close(); } }
public void WriteBackStream(string file, Stream strm) { #if SHARPZIPLIB if (_zipFile == null) { #endif strm.Close(); #if SHARPZIPLIB } else { _zipFile.BeginUpdate(); int nr = _zipFile.FindEntry(file.Replace("\\", "/"), true); _zipFile.Delete(_zipFile[nr]); CustomStaticDataSource sds = new CustomStaticDataSource(); sds.SetStream(strm); _zipFile.Add(sds, file.Replace("\\", "/")); _zipFile.CommitUpdate(); _zipFile.Close(); _zipFile = new ZipFile(_zipFileName); /* * while (_zipFile.IsUpdating) * { * System.Threading.Thread.Sleep(100); * } */ strm.Close(); } #endif }
void makeZip(string fn, string docXml) { var ass = Assembly.GetExecutingAssembly(); using (Stream temaplateStream = ass.GetManifestResourceStream("XiePinyin.Resources.template.docx")) { byte[] ba = new byte[temaplateStream.Length]; temaplateStream.Read(ba, 0, ba.Length); File.WriteAllBytes(fn, ba); } using (Stream zipStream = new FileStream(fn, FileMode.Open, FileAccess.ReadWrite)) { ZipFile zipFile = new ZipFile(zipStream); zipFile.BeginUpdate(); CustomStaticDataSource sds; // Update document.xml sds = new CustomStaticDataSource(); sds.SetStream(new MemoryStream(Encoding.UTF8.GetBytes(docXml))); zipFile.Add(sds, "word/document.xml"); // Update styles.xm sds = new CustomStaticDataSource(); sds.SetStream(new MemoryStream(Encoding.UTF8.GetBytes(skStyles))); zipFile.Add(sds, "word/styles.xml"); // Finish zipFile.CommitUpdate(); zipFile.Close(); } }
private void c_btnCreate_Click(object sender, EventArgs e) { if (c_txtDescription.Text.Length < 20) { Utility.MessageBoxShow("Description must be atleast 20 characters long"); return; } var addonName = c_txtAddonFolder.Text.Split(new char[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries).Last(); int addonNameIndex = c_txtAddonFolder.Text.LastIndexOf(addonName); string addonRootFolder = c_txtAddonFolder.Text.Substring(0, addonNameIndex); SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.DefaultExt = "zip"; saveFileDialog.Filter = "Zip files (*.zip)|*.zip|All files (*.*)|*.*"; saveFileDialog.InitialDirectory = addonRootFolder; saveFileDialog.AddExtension = true; saveFileDialog.FileName = addonName + "Package_" + c_txtVersion.Text + ".zip"; var dialogResult = saveFileDialog.ShowDialog(); if (dialogResult == System.Windows.Forms.DialogResult.OK) { string addonPackageFilename = saveFileDialog.FileName; string descFile = "\r\n#Version=" + c_txtVersion.Text; descFile += "\r\n#Description=" + c_txtDescription.Text.Replace("\r\n#", "\n#"); descFile += "\r\n#UpdateSubmitter=" + Settings.UserID.Split('.').First(); descFile += "\r\n#UpdateImportance=|DefaultImportance=" + (string)c_cbUpdateImportance.SelectedItem + "|"; Utility.AssertFilePath(addonPackageFilename); ZipFile zipFile; if (System.IO.File.Exists(addonPackageFilename) == true) { throw new Exception("The AddonPackage file already exists!"); } zipFile = ZipFile.Create(addonPackageFilename); zipFile.BeginUpdate(); zipFile.AddDirectoryFilesRecursive(addonRootFolder, c_txtAddonFolder.Text); //ZipEntry descEntry = new ZipEntry(addonName + "/VF_WowLauncher_AddonDescription.txt"); //descEntry.DateTime = DateTime.Now; System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(memoryStream); streamWriter.Write(descFile); streamWriter.Flush(); CustomStaticDataSource entryDataSource = new CustomStaticDataSource(); entryDataSource.SetStream(memoryStream); zipFile.Add(entryDataSource, addonName + "/VF_WowLauncher_AddonDescription.txt"); zipFile.CommitUpdate(); zipFile.Close(); Close(); } }
public static void GenerateExtension(string filepath, string host, string port, string username, string password) { if (string.IsNullOrEmpty(filepath)) { throw new ArgumentNullException(nameof(filepath)); } if (string.IsNullOrEmpty(host)) { throw new ArgumentNullException(nameof(host)); } if (string.IsNullOrEmpty(port)) { throw new ArgumentNullException(nameof(port)); } if (string.IsNullOrEmpty(username)) { throw new ArgumentNullException(nameof(username)); } if (string.IsNullOrEmpty(password)) { throw new ArgumentNullException(nameof(password)); } if (File.Exists(filepath)) { try { File.Delete(filepath); } catch (Exception) { } } string background_ = Resource.ProxyLogin_Ext_background .Replace("{host}", host) .Replace("{port}", port.ToString()) .Replace("{username}", username) .Replace("{password}", password); using ZipFile zipFile = ZipFile.Create(filepath); zipFile.BeginUpdate(); using CustomStaticDataSource manifest = new CustomStaticDataSource(Resource.ProxyLogin_Ext_manifest); zipFile.Add(manifest, "manifest.json"); using CustomStaticDataSource background = new CustomStaticDataSource(background_); zipFile.Add(background, "background.js"); zipFile.CommitUpdate(); zipFile.Close(); }
public void addZipEntry(string zipFullName, string entryName, MemoryStream ms) { FileStream sin = null; ZipFile zipFile = null; CustomStaticDataSource sds; try { sin = new FileStream(zipFullName, FileMode.Open); zipFile = new ZipFile(sin); zipFile.BeginUpdate(); sds = new CustomStaticDataSource(); sds.SetStream(ms); zipFile.Add(sds, entryName); zipFile.CommitUpdate(); } catch (Exception ex) { Utility.Logger.myLogger.Error(ex); } finally { if (ms != null) { ms.Close(); ms.Dispose(); } if (zipFile != null) { zipFile.Close(); } if (sin != null) { sin.Close(); sin.Dispose(); } } }
public void Serialize(string path) { var document = new XmlDocument(); document.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\"?><project></project>"); document.DocumentElement.SetAttribute("tempo", tempo.ToString(CultureInfo.InvariantCulture)); foreach (var x in lines) { x.Serialize(document); } foreach (var x in tracks) { x.Serialize(document); } foreach (var x in NoteSynths) { x.Serialize(document.DocumentElement); } var zip = ZipFile.Create(path); CustomStaticDataSource sds = new CustomStaticDataSource(); var str = new MemoryStream(); var writer = new StreamWriter(str); writer.Write(document.OuterXml); writer.Flush(); str.Position = 0; sds.SetStream(str); zip.BeginUpdate(); // If an entry of the same name already exists, it will be overwritten; otherwise added. zip.Add(sds, "project.xml"); zip.CommitUpdate(); zip.Close(); }
public void WriteBackStream(string file, Stream strm) { #if SHARPZIPLIB if (_zipFile == null) { #endif strm.Close(); #if SHARPZIPLIB } else { _zipFile.BeginUpdate(); int nr = _zipFile.FindEntry(file.Replace("\\", "/"), true); _zipFile.Delete(_zipFile[nr]); CustomStaticDataSource sds = new CustomStaticDataSource(); sds.SetStream(strm); _zipFile.Add(sds, file.Replace("\\", "/")); _zipFile.CommitUpdate(); _zipFile.Close(); _zipFile = new ZipFile(_zipFileName); /* while (_zipFile.IsUpdating) { System.Threading.Thread.Sleep(100); } */ strm.Close(); } #endif }
public bool DowngradeSong(bool replaceZKCommand) { ZipFile zipFile = new ZipFile(filename); ZipEntry zipEntry = zipFile.GetEntry("Song.xml"); try { if (zipEntry != null) { XmlDocument doc = new XmlDocument(); Stream stream = zipFile.GetInputStream(zipEntry); MemoryStream msEntry = new MemoryStream(); stream.CopyTo(msEntry); msEntry.Position = 0; doc.Load(msEntry); XmlNode nodeversion = doc.SelectSingleNode("RenoiseSong/GlobalSongData/PlaybackEngineVersion"); if (nodeversion != null) { int version = Int16.Parse(nodeversion.InnerXml); if (version != 1) { if (replaceZKCommand) { XmlNodeList list = doc.SelectNodes("//Lines/Line/EffectColumns/EffectColumn[Number='ZK']/Number"); list.Cast <XmlNode>().Select(o => o.InnerXml = "ZL").ToList(); } nodeversion.InnerXml = "1"; zipFile.BeginUpdate(); CustomStaticDataSource sds = new CustomStaticDataSource(); sds.SetStream(msEntry); doc.Save(sds.GetSource()); sds.GetSource().Position = 0; zipFile.Add(sds, zipEntry.Name); zipFile.CommitUpdate(); } else { throw new XrnsException(DOWNGRADE_ALREADY_DONE); } } else { throw new XrnsException(READING_ERROR); } } else { throw new XrnsException(READING_ERROR); } } finally { if (zipFile != null) { zipFile.Close(); } } return(true); }
public void TranslateAndUpdatingZip(string path, string searchPattern) { List <ZipEntity> list_en_US = new List <ZipEntity>(); List <ZipEntity> list_zh_CN = new List <ZipEntity>(); List <ZipEntity> list_zh_TW = new List <ZipEntity>(); if (!Directory.Exists(path)) { MessageBox.Show("找不到資料夾"); return; } string[] fileList = Directory.GetFiles(path, searchPattern, SearchOption.AllDirectories); //建立清單 foreach (string zipFile in fileList) { if (File.Exists(zipFile)) { using (ZipFile zip = new ZipFile(zipFile)) { Regex r = new Regex("(en_US)|(zh_CN)|(zh_TW)", RegexOptions.IgnoreCase); foreach (ZipEntry entry in zip) { if (entry.IsFile) { var m = r.Match(entry.Name); if (m.Groups.Count > 1) { if (m.Groups[1].Length != 0) { list_en_US.Add(new ZipEntity() { ZipPath = zipFile, Name = entry.Name }); } else if (m.Groups[2].Length != 0) { list_zh_CN.Add(new ZipEntity() { ZipPath = zipFile, Name = entry.Name }); } else if (m.Groups[3].Length != 0) { list_zh_TW.Add(new ZipEntity() { ZipPath = zipFile, Name = entry.Name }); } } } } } } } /*取得副檔名清單 * List<string> ggggg = new List<string>(); * list_zh_CN.ForEach(x => ggggg.Add(Path.GetExtension(x.Name))); * ggggg = ggggg.Distinct().ToList(); * foreach (var jj in ggggg) * System.Diagnostics.Debug.Print(list_zh_CN.Find(x => Path.GetExtension(x.Name) == jj).Name);*/ var needUpdatingFiles = list_zh_CN.Select(x => x.ZipPath).Distinct(); foreach (var FileListTemp in needUpdatingFiles) { bool IsIgnore = false; while (!IsIgnore && PrivateFunction.FileStatusHelper.IsFileOccupied(FileListTemp)) { switch (MessageBox.Show(FileListTemp + "\n該檔案已被占用,請自行關閉該檔案,是否繼續?", "檔案被占用啦!!", MessageBoxButtons.AbortRetryIgnore)) { case DialogResult.Abort: return; case DialogResult.Ignore: IsIgnore = true; break; } } if (IsIgnore) { continue; } var temp = list_zh_CN.Where(x => x.ZipPath == FileListTemp); using (ZipFile zip = new ZipFile(FileListTemp)) { zip.BeginUpdate(); foreach (var entity in temp) { Stream zipStream_CN = zip.GetInputStream(zip.GetEntry(entity.Name)); // Manipulate the output filename here as desired. using (StreamReader sr_CN = new StreamReader(zipStream_CN, Encoding.UTF8)) { StringBuilder sb_CN = new StringBuilder(); if (checkedListBox1.GetItemChecked(0)) { sb_CN.AppendLine(CC.Convert(PrivateFunction.ChineseConverter.ToTraditional(sr_CN.ReadToEnd()))); } else { sb_CN.AppendLine(PrivateFunction.ChineseConverter.ToTraditional(sr_CN.ReadToEnd())); } sr_CN.Close(); if ((!checkedListBox1.GetItemChecked(1)) && (!checkedListBox1.GetItemChecked(2))) { MemoryStream ms2 = new MemoryStream(Encoding.UTF8.GetBytes(sb_CN.ToString())); CustomStaticDataSource sds = new CustomStaticDataSource(); sds.SetStream(ms2); zip.Add(sds, entity.Name.Replace("zh_CN", "zh_TW").Replace("zh_cn", "zh_tw")); ms2.Close(); continue; } if (entity.Name.Contains("blankpattern.json")) { Stream zipStream_CN2 = zip.GetInputStream(zip.GetEntry(entity.Name)); StreamReader sr_CNs = new StreamReader(zipStream_CN2, Encoding.UTF8); System.Diagnostics.Debug.Print(CC.Convert(PrivateFunction.ChineseConverter.ToTraditional(sr_CNs.ReadToEnd()))); } string stringOutput = ""; //簡單判斷是否是ini格式 if (Path.GetExtension(entity.Name).Replace(".properties", "").Replace(".lang", "") == "") { Ini ini_EN = new Ini(), ini_CN = new Ini(), ini_TW = new Ini(); PrivateFunction.ReadAllIniFile(sb_CN.ToString(), ref ini_CN); //讀取zh_TW if (checkedListBox1.GetItemChecked(1) && list_zh_TW.Exists(x => x.ZipPath == FileListTemp && x.Name == entity.Name.Replace("zh_CN", "zh_TW").Replace("zh_cn", "zh_tw"))) { Stream zipStream_TW = zip.GetInputStream(zip.GetEntry(entity.Name.Replace("zh_CN", "zh_TW").Replace("zh_cn", "zh_tw"))); using (StreamReader sr_TW = new StreamReader(zipStream_TW, Encoding.UTF8)) { PrivateFunction.ReadAllIniFile(sr_TW, ref ini_TW); sr_TW.Close(); zipStream_TW.Close(); } } //讀取en_US if (checkedListBox1.GetItemChecked(2) && list_en_US.Exists(x => x.ZipPath == FileListTemp && x.Name == entity.Name.Replace("zh_CN", "en_US").Replace("zh_cn", "en_us"))) { Stream zipStream_EN = zip.GetInputStream(zip.GetEntry(entity.Name.Replace("zh_CN", "en_US").Replace("zh_cn", "en_us"))); using (StreamReader sr_EN = new StreamReader(zipStream_EN, Encoding.UTF8)) { PrivateFunction.ReadAllIniFile(sr_EN, ref ini_EN); sr_EN.Close(); zipStream_EN.Close(); } } List <Line> tempLineList = new List <Line>(); //如果有簡體又有繁體,則在暫存加入繁體的 foreach (Line l in ini_CN.Lines) { var Ltemp = ini_TW.Lines.Find(x => x.Section == l.Section && x.Key == l.Key); if (Ltemp != null) { tempLineList.Add(Ltemp); } } //如果只有簡體沒有繁體,加入簡體 foreach (Line l in ini_CN.Lines) { if (!ini_TW.Lines.Exists(x => x.Section == l.Section && x.Key == l.Key)) { tempLineList.Add(l); } } //如果只有繁體沒有簡體,加入繁體 foreach (Line l in ini_TW.Lines) { if (!ini_CN.Lines.Exists(x => x.Section == l.Section && x.Key == l.Key)) { tempLineList.Add(l); } } //如果有英文沒有中文,則在暫存加入英文的 foreach (Line l in ini_EN.Lines) { if (!tempLineList.Exists(x => x.Section == l.Section && x.Key == l.Key)) { tempLineList.Add(l); } } //雙語翻譯 /*由於.lang中不能換行,因此暫時失敗 * if (checkedListBox1.GetItemChecked(3)) * foreach (Line l in ini_EN.Lines) * { * var t = tempLineList.FirstOrDefault(x => x.Section == l.Section && x.Key == l.Key && !x.Value.EndsWith(l.Value)); * if (t != null) * { * //%s不能重複使用,因此需要特別處理 * int sCount = (l.Value.Length - l.Value.Replace("%s", "").Length) / 2; * if (sCount > 0) * { * string[] stringtemp = l.Value.Split(new string[] { "%s" }, StringSplitOptions.None); * int i = 0; * StringBuilder sb = new StringBuilder(l.Value.Length + sCount * 2); * foreach (var s in stringtemp) * { * sb.Append(s); * i++; * if (i < stringtemp.Length) * sb.AppendFormat("%{0}$s", i); * } * t.Value = String.Format("{0}\\n{1}", t.Value, sb.ToString()); * } * else * t.Value = String.Format("{0}\\n{1}", t.Value, l.Value); * } * }*/ stringOutput = PrivateFunction.IniConvertToText(tempLineList); } else { stringOutput = sb_CN.ToString(); } MemoryStream ms_output = new MemoryStream(Encoding.UTF8.GetBytes(stringOutput)); CustomStaticDataSource sdss = new CustomStaticDataSource(); sdss.SetStream(ms_output); zip.Add(sdss, entity.Name.Replace("zh_CN", "zh_TW").Replace("zh_cn", "zh_tw")); //ms_output.Close(); } } try { zip.CommitUpdate(); } catch (Exception e1) { MessageBox.Show(String.Format("{0}寫入失敗\n\n錯誤資訊如下:\n\n{1}\n\n請稍後再嘗試", FileListTemp, e1.Message)); } zip.Close(); } } }