using System; using System.Collections; using java.util; //引用J# vjslib.dll using java.util.zip;//引用J# vjslib.dll namespace FrameWork2005.DataAccess { /// <summary> /// 非泛型枚举委托 /// </summary> /// <returns></returns> public delegate Enumeration EnumerationMethod(); /// <summary> /// 包装 java 枚举 /// </summary> public class EnumerationAdapter : IEnumerable { private class EnumerationWrapper : IEnumerator { private EnumerationMethod m_Method; private Enumeration m_Wrapped; private object m_Current; public EnumerationWrapper(EnumerationMethod method) { m_Method = method; } // IEnumerator public object Current { get { return m_Current; } } public void Reset() { m_Wrapped = m_Method(); if (m_Wrapped == null) throw new InvalidOperationException(); } public bool MoveNext() { if (m_Wrapped == null) Reset(); bool Result = m_Wrapped.hasMoreElements(); if (Result) m_Current = m_Wrapped.nextElement(); return Result; } } private EnumerationMethod m_Method; public EnumerationAdapter(EnumerationMethod method) { if (method == null) throw new ArgumentException(); m_Method = method; } // IEnumerable public IEnumerator GetEnumerator() { return new EnumerationWrapper(m_Method); } } /// <summary> /// 过滤入口委托方法 /// </summary> /// <param name="e">zip入口</param> /// <returns></returns> public delegate bool FilterEntryMethod(ZipEntry e); /// <summary> /// Zip流工具类 /// </summary> public class ZipUtils { private static string _fname; /// <summary> /// zip文件路劲和文件名,添加zip时使用 /// </summary> public static string fname { set { _fname = value; } get { return _fname; } } private static ZipFile _CurrentFile; /// <summary> /// 声明ZipFile类,需要fname支持 /// </summary> public static ZipFile CurrentFile { get { if (!string.IsNullOrEmpty(_fname)) { try { _CurrentFile = new ZipFile(fname); } catch(Exception ex) { throw ex; } } return _CurrentFile; } set { if (_CurrentFile != null) _CurrentFile.close(); _CurrentFile = value; } } /// <summary> /// 复制流 /// </summary> /// <param name="from"></param> /// <param name="to"></param> public static void CopyStream(java.io.InputStream from, java.io.OutputStream to) { sbyte[] buffer = new sbyte[8192]; int got; while ((got = from.read(buffer, 0, buffer.Length)) > 0) to.write(buffer, 0, got); } /// <summary> /// 提取zip中的文件 /// </summary> /// <param name="file"></param> /// <param name="path"></param> /// <param name="filter"></param> public static void ExtractZipFile(ZipFile file, string path, FilterEntryMethod filter) { foreach(ZipEntry entry in new EnumerationAdapter(new EnumerationMethod(file.entries))) { if (!entry.isDirectory()) { if ((filter == null || filter(entry))) { java.io.InputStream s = file.getInputStream(entry); try { string fname = System.IO.Path.GetFileName(entry.getName()); string newpath = System.IO.Path.Combine(path, System.IO.Path.GetDirectoryName(entry.getName())); System.IO.Directory.CreateDirectory(newpath); java.io.FileOutputStream dest = new java.io.FileOutputStream(System.IO.Path.Combine(newpath, fname)); try { CopyStream(s, dest); } finally { dest.close(); } } finally { s.close(); } } } } } /// <summary> /// 产生一个新的空zip /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static ZipFile CreateEmptyZipFile(string fileName) { new ZipOutputStream(new java.io.FileOutputStream(fileName)).close(); return new ZipFile(fileName); } /// <summary> /// 向已有zip中添加 /// </summary> /// <param name="file"></param> /// <param name="filter"></param> /// <param name="newFiles"></param> /// <returns></returns> public static ZipFile UpdateZipFile(ZipFile file, FilterEntryMethod filter, string[] newFiles) { string prev = file.getName(); string tmp = System.IO.Path.GetTempFileName(); ZipOutputStream to = new ZipOutputStream(new java.io.FileOutputStream(tmp)); try { CopyEntries(file, to, filter); // 添加动作 if (newFiles != null) { foreach(string f in newFiles) { //ZipEntry z = new ZipEntry(f.Remove(0, System.IO.Path.GetPathRoot(f).Length)); ZipEntry z = new ZipEntry(System.IO.Path.GetFileName(f)); z.setMethod(ZipEntry.DEFLATED); to.putNextEntry(z); try { java.io.FileInputStream s = new java.io.FileInputStream(f); try { CopyStream(s, to); } finally { s.close(); } } finally { to.closeEntry(); } } } } finally { to.close(); } file.close(); // 添加完成替换旧有文件 System.IO.File.Copy(tmp, prev, true); System.IO.File.Delete(tmp); return new ZipFile(prev); } /// <summary> /// 复制已有记录 /// </summary> /// <param name="from"></param> /// <param name="to"></param> public static void CopyEntries(ZipFile from, ZipOutputStream to) { CopyEntries(from, to, null); } /// <summary> /// 复制记录 /// </summary> /// <param name="from"></param> /// <param name="to"></param> /// <param name="filter"></param> public static void CopyEntries(ZipFile from, ZipOutputStream to, FilterEntryMethod filter) { foreach(ZipEntry entry in new EnumerationAdapter(new EnumerationMethod(from.entries))) { if (filter == null || filter(entry)) { java.io.InputStream s = from.getInputStream(entry); try { to.putNextEntry(entry); try { CopyStream(s, to); } finally { to.closeEntry(); } } finally { s.close(); } } } } } } 发现c#的多语言平台的好处,可以调用java包处理zip功能! 特收藏用