// byte[] = byte[] /// <summary> /// 压缩后DES加密 /// </summary> /// <param name="hexStringKey"></param> /// <param name="byteSource"></param> /// <returns></returns> public static byte[] ZipAndDelphiDes(string hexStringKey, byte[] byteSource) { string hexStringIV = StringEncode.ByteArrayToHexString(DefaultDesIV); byte[] zipResult = ZipCompress.Compress(byteSource); return(DelphiDesEncrypt(hexStringKey, hexStringIV, zipResult)); }
private static string ZipXlsFiles(List <string> fileList) { string zipFileName = Guid.NewGuid().ToString() + ".zip"; string zipFilePath = RunDirectory.LinkTempPath(zipFileName); ZipCompress zip = new ZipCompress(); zip.BeginZip(zipFilePath); try { foreach (string xlsFileName in fileList) { string filePath = RunDirectory.LinkTempPath(xlsFileName); try { zip.AddFileToZip(filePath, xlsFileName); } finally { File.Delete(filePath); } } } finally { zip.FinishZip(); } return(zipFileName); }
/// <summary> /// DES解密后解压 /// </summary> /// <param name="hexStringKey"></param> /// <param name="encryptSource"></param> /// <returns></returns> public static byte[] UnDelphiDesAndUnZip(string hexStringKey, byte[] encryptSource) { string hexStringIV = StringEncode.ByteArrayToHexString(DefaultDesIV); byte[] desResult = DelphiDesDecryptToArray(hexStringKey, hexStringIV, encryptSource); return(ZipCompress.DeCompress(desResult)); }
private void btnUnZip_Click(object sender, EventArgs e) { if (rbGZip.Checked) { txt23.Text = GZipCompress.DeCompressStringFromBase64(txt22.Text); } else { txt23.Text = ZipCompress.DeCompressStringFromBase64(txt22.Text); } }
private void btnZipToHex_Click(object sender, EventArgs e) { if (rbGZip.Checked) { txt22.Text = GZipCompress.CompressStringToHex(txt21.Text); } else { txt22.Text = ZipCompress.CompressStringToHex(txt21.Text); } }
public void ZipCompressTest() { var test = @"hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello"; var sb = new StringBuilder(test); var stream = StreamHeandler.StringToStream(sb.ToString()); var zipStream = ZipCompress.Compress(stream); var unZipStream = ZipCompress.UnCompress(zipStream); var sb2 = StreamHeandler.StreamToString(stream); Assert.AreEqual(sb.ToString(), sb2); }
public List <YHDZJL> GetYHDZJL(string merchant_no, string chkDate) { List <YHDZJL> ret = new List <YHDZJL>(); ReportFormResponse t = new ReportFormResponse() { merchant_no = merchant_no, chkDate = chkDate }; ReportFormRequest res = Post <ReportFormResponse, ReportFormRequest>(Url, t); if (res.return_code != "00") { throw new Exception(res.return_msg); } using (Stream st = new MemoryStream(Convert.FromBase64String(res.fileData))) { IOExtension.GetTempPathAndDo(path => { CompressBase cm = new ZipCompress(); cm.DeCompression(st, path); IOExtension.GetAllFiles(path).ForEach(file => { ret = TxtReader.ReadToModel <YHDZJL>(new TableReaderSettings() { ColumnSplit = new string[] { "|" }, FilePath = file.FullName, RowSplit = new string[] { "\n" }, RowSettings = new Dictionary <int, string>() //这里处理格式 { { 1, "SHBH" }, { 2, "ZDBH" }, { 3, "YHKH" }, { 4, "JYJE" }, { 5, "SXF" }, { 6, "JYRQ" }, { 7, "JYSJ" }, { 8, "JYLBH" }, { 9, "JYLBM" }, { 10, "QFSJ" }, { 11, "JGM" }, { 12, "ZFFS" }, { 13, "LSBH" } } }); }); }); } return(ret); }
private void ProcessCompressAttachment(XElement root, string tempDir, string filepath, int idparent, ref int id) { using (FileStream stream = File.OpenRead(filepath)) { bool isRar = Path.GetExtension(filepath).ToLower().EndsWith(".rar"); ICollection <CompressItem> results = null; if (isRar) { results = new RarCompress().InMemoryExtract(stream); } else { results = new ZipCompress().InMemoryExtract(stream); } foreach (CompressItem item in results) { string outpath = Utils.CreateTempName(tempDir, item.Filename, _biblosMaxLength); XElement child = new XElement("file", outpath); //parent XAttribute childParent = new XAttribute("idParent", idparent.ToString(CultureInfo.InvariantCulture)); child.Add(childParent); //id XAttribute childId = new XAttribute("id", id++.ToString(CultureInfo.InvariantCulture)); child.Add(childId); //sourcename XAttribute childSource = new XAttribute("sourceName", item.Filename); child.Add(childSource); root.Add(child); File.WriteAllBytes(outpath, item.Data); //inner zip string extension = Path.GetExtension(outpath); if (extension != null && CompressExtensions.Contains(extension.ToLower())) { ProcessCompressAttachment(root, tempDir, outpath, id - 1, ref id); } } } }
public void StreamHeandlerTest() { var text1 = @"hello who"; var stream = StreamHeandler.StringToStream(text1); var zipStream = ZipCompress.Compress(stream); var base64 = Base64Convert.ToBase64(zipStream); var txt1 = StreamHeandler.StreamToString(base64); var txt2 = txt1; var base642 = StreamHeandler.StringToStream(txt2); var unZipStream = Base64Convert.FrmBase64(base642); var steam2 = ZipCompress.UnCompress(unZipStream); var text2 = StreamHeandler.StreamToString(steam2); Assert.AreEqual(text1, text2); }
static CompressHelper() { // 创建使用SharpZipLib的二个委托。 Type t = Type.GetType("ICSharpCode.SharpZipLib.BZip2.BZip2, ICSharpCode.SharpZipLib", false); if (t != null) { MethodInfo m1 = t.GetMethod("Compress", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(Stream), typeof(Stream), typeof(bool), typeof(int) }, null); MethodInfo m2 = t.GetMethod("Decompress", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(Stream), typeof(Stream), typeof(bool) }, null); if (m1 != null && m2 != null) { s_ZipCompressAction = Delegate.CreateDelegate(typeof(ZipCompress), m1) as ZipCompress; s_ZipDecompressAction = Delegate.CreateDelegate(typeof(ZipDecompress), m2) as ZipDecompress; } } }
/// <summary> /// 获取数据 /// </summary> /// <param name="parameter"></param> /// <returns></returns> public DataResult PullData(DataParameter parameter) { LogHelper.Debug(GetType(), "准备获取数据", LogInfo.ObjectToMessage(parameter)); var success = true; var result = new DataResult(); var crchash = new CrcHash(); PHPRPC_Client client = new PHPRPC_Client("http://localhost:4631/PullData.aspx"); IPullDataService pullDataService = (IPullDataService)client.UseService(typeof(IPullDataService)); //var pullDataService = AppCtx.Resolve<IPullDataService>("PullService"); //初始化异常 if (pullDataService == null) { LogHelper.Debug(GetType(), "获取接口失败"); return(null); } #region 开始请求 var beginParameter = new DataParameter(); beginParameter.Action = parameter.Action; LogHelper.Debug(GetType(), "发送请求", LogInfo.ObjectToMessage(beginParameter)); var beginDataInfo = pullDataService.Begin(beginParameter); if (beginDataInfo == null) { success = false; result.ErrorId = (int)DataDictionary.ErrorType.DataPullBeginError; result.ErrorMessage = ""; return(result); } else { result.Action = parameter.Action; result.SplitCount = beginDataInfo.SpliteCount; } #endregion #region 请求数据 //初始化容器以收集字符串 var sb = new StringBuilder(beginDataInfo.SpliteCount * dataLength); LogHelper.Debug(GetType(), "开始请求具体数据"); for (int i = 0; i < beginDataInfo.SpliteCount; i++) { //请求数据 //todo:需优化,以重新请求超时未能请求到的数据 //var tcs = new TaskCompletionSource<DataSegment>(); //WaitCallback asyncWork = _ => //{ var dataSegment = pullDataService.Pull(beginDataInfo.DataId, i); if (dataSegment == null) { //tcs.SetResult(null); } else { LogHelper.Debug(GetType(), string.Format("获取索引为{0}的数据:", i), LogInfo.ObjectToMessage(dataSegment)); var stream = StreamHeandler.StringToStream(dataSegment.DataString); dataSegment = !crchash.Hash(stream, dataSegment.Crccode) ? null : dataSegment; } // tcs.SetResult(dataSegment); // } //}; //ThreadPool.QueueUserWorkItem(asyncWork); //var task = tcs.Task; //var taskResult = task.Result; if (dataSegment != null) { sb.Insert(i * dataLength, dataSegment.DataString); } else { result.ErrorId = (int)DataDictionary.ErrorType.DataPullError; result.ErrorMessage = ""; success = false; break; } } #endregion #region 结束请求 var endData = pullDataService.End(beginDataInfo.DataId, !success); LogHelper.Debug(GetType(), "结束请求数据", LogInfo.ObjectToMessage(endData)); if (endData == null) { result.ErrorId = (int)DataDictionary.ErrorType.DataPullEndError; result.ErrorMessage = ""; success = false; } #endregion if (success) { var stream = StreamHeandler.StringToStream(sb.ToString()); var base64 = Base64Convert.FrmBase64(stream); LogHelper.Debug(GetType(), "解压请求到的数据"); var unZipStream = ZipCompress.UnCompress(base64); result.CrcCode = crchash.GetHashCode(unZipStream); LogHelper.Debug(GetType(), "生成校验码", result.CrcCode); result.DataString = StreamHeandler.StreamToString(unZipStream); //返回 } return(result); }
/// <summary> /// 发送数据 /// </summary> /// <param name="parameter"></param> /// <returns></returns> public DataResult PushData(DataParameter parameter) { LogHelper.Debug(GetType(), "准备发送数据", LogInfo.ObjectToMessage(parameter)); PHPRPC_Client client = new PHPRPC_Client("http://localhost:4631/PullData.aspx"); IPushDataService pushDataService = (IPushDataService)client.UseService(typeof(IPushDataService)); //var pushDataService = AppCtx.Resolve<IPushDataService>("PushService"); //初始化异常 if (pushDataService == null) { LogHelper.Warn(GetType(), "获取接口失败"); return(null); } var success = true; var result = new DataResult(); var beginDataInfo = new DataInfo(); var crchash = new CrcHash(); result.DataId = Guid.NewGuid().ToString(); result.Action = parameter.Action; result.CrcCode = parameter.CrcCode; result.DataString = parameter.DataString; var stream = StreamHeandler.StringToStream(parameter.DataString); var zipStream = ZipCompress.Compress(stream); var base64 = Base64Convert.ToBase64(zipStream); var sb = new StringBuilder(StreamHeandler.StreamToString(base64)); var count = (int)Math.Ceiling(sb.Length / (double)dataLength); result.SplitCount = count; #region 准备发送数据 beginDataInfo.ActionId = parameter.Action; beginDataInfo.DataId = result.DataId; beginDataInfo.SpliteCount = count; LogHelper.Debug(GetType(), "开始请求发送数据", LogInfo.ObjectToMessage(beginDataInfo)); pushDataService.Begin(beginDataInfo); #endregion #region 发送数据 LogHelper.Debug(GetType(), "开始发送数据"); for (int i = 0; i < count; i++) { var tcs = new TaskCompletionSource <bool>(); WaitCallback asyncWork = _ => { var beginPos = i * dataLength; var legth = i == count - 1 ? sb.Length - beginPos : dataLength; var data = sb.ToString(beginPos, legth); var dataSegment = new DataSegment(); dataSegment.DataId = result.DataId; dataSegment.Crccode = crchash.GetHashCode(StreamHeandler.StringToStream(data)); dataSegment.DataString = data; dataSegment.Index = i; LogHelper.Debug(GetType(), string.Format("发送数据,ID:{0},索引:{1}", result.DataId, i), LogInfo.ObjectToMessage(dataSegment)); pushDataService.Push(dataSegment); tcs.SetResult(true); }; ThreadPool.QueueUserWorkItem(asyncWork); var task = tcs.Task; var taskResult = task.Result; if (!taskResult) { success = false; break; } } #endregion #region 发送完成 var endResult = pushDataService.End(beginDataInfo.DataId, !success); LogHelper.Debug(GetType(), "发送数据完成"); if (endResult == null) { success = false; } #endregion return(result); }
internal OneThroughNine(ZipCompress zipCompress) { _zipCompress = zipCompress; }
// byte[] = byte[] /// <summary> /// 3DES解密后Zip解压缩,密钥长度必需是24字节 /// </summary> /// <param name="hexStringKey">密钥串</param> /// <param name="encryptSource"></param> /// <returns></returns> public static byte[] Un3DesAndUnZip(string hexStringKey, byte[] encryptSource) { byte[] desResult = Cryptography.TripleDesDecrypt(hexStringKey, encryptSource); return(ZipCompress.DeCompress(desResult)); }
// byte[] = byte[] /// <summary> /// Zip压缩后3DES加密,密钥长度必需是24字节 /// </summary> /// <param name="hexStringKey">密钥</param> /// <param name="source"></param> /// <returns></returns> public static byte[] ZipAnd3Des(string hexStringKey, byte[] source) { byte[] zipResult = ZipCompress.Compress(source); return(Cryptography.TripleDesEncrypt(hexStringKey, zipResult)); }
/// <summary> /// 替换程序 /// </summary> public void Replace() { string fileName = ServiceFileInfoPath + @"\" + this.FileName; //是压缩包 if (this.IsZip) { if (!System.IO.Directory.Exists(TempZipPath)) { System.IO.Directory.CreateDirectory(TempZipPath); } if (!System.IO.Directory.Exists(TempBakPath)) { System.IO.Directory.CreateDirectory(TempBakPath); } //Zip包解压缩,并且替换目标文件 Message?.Invoke("正在解压..."); if (ZipCompress == null) { ZipCompress = new GZipCompress(); } ZipCompress.DirPath = TempZipPath; ZipCompress.ZipFileName = fileName; ZipCompress.Message = Message; ZipCompress.DeCompress(); System.Threading.Thread.Sleep(100); //复制文件到安装目录 var tempFiles = System.IO.Directory.GetFiles(TempZipPath, "*", System.IO.SearchOption.AllDirectories); foreach (string f in tempFiles) { Message?.Invoke("正在替换文件" + f + "..."); string workPath = System.IO.Path.GetFullPath(LocalFileInfoPath + @"\" + f.Substring(TempZipPath.Length, f.Length - TempZipPath.Length - System.IO.Path.GetFileName(f).Length)); if (!System.IO.Directory.Exists(workPath)) { System.IO.Directory.CreateDirectory(workPath); } string strOldFilename = System.IO.Path.GetFullPath(workPath + @"\" + System.IO.Path.GetFileName(f)); string strOldBakFilename = System.IO.Path.GetFullPath(workPath.Replace(LocalFileInfoPath, TempBakPath) + @"\" + System.IO.Path.GetFileName(f)); if (System.IO.File.Exists(strOldFilename)) { if (System.IO.File.Exists(strOldBakFilename + ".bak")) { System.IO.File.Delete(strOldBakFilename + ".bak"); } try { System.IO.File.Move(strOldFilename, strOldBakFilename + ".bak"); } catch (Exception ex) { ex.ToString(); } } try { System.IO.File.Copy(f, System.IO.Path.GetFullPath(workPath + @"\" + System.IO.Path.GetFileName(f)), true); } catch (Exception ex) { ex.ToString(); } } System.Threading.Thread.Sleep(100); Message?.Invoke("正在清理..."); System.IO.File.Delete(fileName); System.IO.File.Delete(ServiceFileInfoPath + @"\" + FileInfoName); System.IO.Directory.Delete(TempZipPath, true); System.IO.Directory.Delete(ServiceFileInfoPath, true); //var fileBaks = System.IO.Directory.GetFiles(LocalFileInfoPath, "*.bak", System.IO.SearchOption.AllDirectories); var fileBaks = System.IO.Directory.GetFiles(TempBakPath, "*.bak", System.IO.SearchOption.AllDirectories); foreach (var fileBak in fileBaks) { try { System.IO.File.Delete(fileBak); } catch (Exception ex) { ex.ToString(); } } System.Threading.Thread.Sleep(100); //var dev = @"cmd.exe"; //System.Diagnostics.Process.Start(dev); } else //是安装包 { System.Diagnostics.Process.Start(fileName); } }