代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
C#压缩单个zip格式文件
作者:
DDT
/ 发布于
2012/1/29
/
1087
<div>?using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.IO; using System.Reflection;</div> <div>namespace ZipArchiveTest { static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false);</div> <div> var form = new Form { Text = "ZipArchiveTest", StartPosition = FormStartPosition.WindowsDefaultBounds }; var listbox = new ListBox { Dock = DockStyle.Fill, IntegralHeight = false }; listbox.MouseDoubleClick += (object p, MouseEventArgs e) => { var path = listbox.SelectedItem as string; if (path == null) return; try { using (var zip = ZipArchive.OpenOnFile((string)listbox.Tag)) { var file = zip.GetFile(path); if (file.FolderFlag) return; var text = new StreamReader(file.GetStream()).ReadToEnd(); var dlg = new Form { Text = Path.Combine((string)listbox.Tag,path), StartPosition = FormStartPosition.WindowsDefaultBounds }; var textbox = new TextBox { Text = text, MaxLength = 0, Multiline = true, ScrollBars = System.Windows.Forms.ScrollBars.Vertical, Dock = DockStyle.Fill }; textbox.Select(0, 0); dlg.Controls.Add(textbox); dlg.ShowDialog(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } };</div> <div> form.Controls.Add(listbox); form.Controls.Add(new ToolStrip(new ToolStripMenuItem("Open Zip Archive...", null, (object p, EventArgs e) => { try { var dlg = new OpenFileDialog { DefaultExt = "zip", Filter = "Zip Archives (*.zip)|*.zip" }; if (dlg.ShowDialog() != DialogResult.OK) return;</div> <div> listbox.Items.Clear(); listbox.Tag = dlg.FileName; using (var zip = ZipArchive.OpenOnFile(dlg.FileName)) listbox.Items.AddRange(zip.Files.OrderBy(file => file.Name).Select(file => file.Name).ToArray()); form.Text = dlg.FileName + " - " + "ZipArchiveTest"; } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } }))); Application.Run(form); } } <div> class ZipArchive : IDisposable { private object external; private ZipArchive() { } public enum CompressionMethodEnum { Stored, Deflated }; public enum DeflateOptionEnum { Normal, Maximum, Fast, SuperFast }; //... public static ZipArchive OpenOnFile(string path, FileMode mode = FileMode.Open, FileAccess access = FileAccess.Read, FileShare share = FileShare.Read, bool streaming = false) { var type = typeof(System.IO.Packaging.Package).Assembly.GetType("MS.Internal.IO.Zip.ZipArchive"); var meth = type.GetMethod("OpenOnFile", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); return new ZipArchive { external = meth.Invoke(null, new object[] { path, mode, access, share, streaming }) }; } public static ZipArchive OpenOnStream(Stream stream, FileMode mode = FileMode.OpenOrCreate, FileAccess access = FileAccess.ReadWrite, bool streaming = false) { var type = typeof(System.IO.Packaging.Package).Assembly.GetType("MS.Internal.IO.Zip.ZipArchive"); var meth = type.GetMethod("OpenOnStream", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); return new ZipArchive { external = meth.Invoke(null, new object[] { stream, mode, access, streaming }) }; } public ZipFileInfo AddFile(string path, CompressionMethodEnum compmeth = CompressionMethodEnum.Deflated, DeflateOptionEnum option = DeflateOptionEnum.Normal) { var type = external.GetType(); var meth = type.GetMethod("AddFile", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); var comp = type.Assembly.GetType("MS.Internal.IO.Zip.CompressionMethodEnum").GetField(compmeth.ToString()).GetValue(null); var opti = type.Assembly.GetType("MS.Internal.IO.Zip.DeflateOptionEnum").GetField(option.ToString()).GetValue(null); return new ZipFileInfo { external = meth.Invoke(external, new object[] { path, comp, opti }) }; } public void DeleteFile(string name) { var meth = external.GetType().GetMethod("DeleteFile", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); meth.Invoke(external, new object[] { name }); } public void Dispose() { ((IDisposable)external).Dispose(); } public ZipFileInfo GetFile(string name) { var meth = external.GetType().GetMethod("GetFile", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); return new ZipFileInfo { external = meth.Invoke(external, new object[] { name }) }; } <div> public IEnumerable<ZipFileInfo> Files { get { var meth = external.GetType().GetMethod("GetFiles", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); var coll = meth.Invoke(external, null) as System.Collections.IEnumerable; //ZipFileInfoCollection foreach (var p in coll) yield return new ZipFileInfo { external = p }; } } public IEnumerable<string> FileNames { get { return Files.Select(p => p.Name).OrderBy(p => p); } } <div> public struct ZipFileInfo { internal object external; private object GetProperty(string name) { return external.GetType().GetProperty(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).GetValue(external, null); } public override string ToString() { return Name;// base.ToString(); } public string Name { get { return (string)GetProperty("Name"); } } public DateTime LastModFileDateTime { get { return (DateTime)GetProperty("LastModFileDateTime"); } } public bool FolderFlag { get { return (bool)GetProperty("FolderFlag"); } } public bool VolumeLabelFlag { get { return (bool)GetProperty("VolumeLabelFlag"); } } public object CompressionMethod { get { return GetProperty("CompressionMethod"); } } public object DeflateOption { get { return GetProperty("DeflateOption"); } } public Stream GetStream(FileMode mode = FileMode.Open, FileAccess access = FileAccess.Read) { var meth = external.GetType().GetMethod("GetStream", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); return (Stream)meth.Invoke(external, new object[] { mode, access }); } } } <div>} </div>
试试其它关键字
压缩单个zip格式
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
DDT
贡献的其它代码
(
160
)
.
Oracle统计表的数据行和数据块信息
.
html标签闭合检测与修复
.
Powershell日期计算
.
Powershell的Base64编解码
.
Powershell并行循环
.
Powershell目录中搜索文本
.
Powershell枚举远程机器上的本地权限组
.
VBScript解析csv文件
.
快速排序之Powershell
.
批处理输出格式化时间字符串
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3