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(); } } } }
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); // add entries here if (newFiles != null) { foreach (string f in newFiles) { ZipEntry z = new ZipEntry(f.Remove(0, System.IO.Path.GetPathRoot(f).Length)); 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(); // now replace the old file with the new one System.IO.File.Copy(tmp, prev, true); System.IO.File.Delete(tmp); return(new ZipFile(prev)); }
/// <summary> /// Extract the content of a zip file passed as a Base64 string /// </summary> /// <param name="b64String"></param> /// <param name="path"></param> /// <param name="filter"></param> public static void ExtractZipFileFromBase64String( string b64String, string path, FilterEntryMethod filter = null) { string tempZipFile = TempFile.GetTempFileName("zip"); // temp file for zip file FileUtil.WriteFileFromBase64String(b64String, tempZipFile); ZipFile zf = new ZipFile(tempZipFile); ExtractZipFile(zf, path, filter); FileUtil.DeleteFile(tempZipFile); return; }
/// <summary> /// Extract files from a ZipFile to a file folder /// </summary> /// <param name="file"></param> /// <param name="path"></param> /// <param name="filter"></param> public static void ExtractZipFile( ZipFile file, string path, FilterEntryMethod filter = null) { 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(); } } } } }