List of usage examples for java.util.zip ZipEntry ZipEntry
public ZipEntry(ZipEntry e)
From source file:Main.java
public void doZip(String filename, String zipfilename) throws Exception { byte[] buf = new byte[1024]; FileInputStream fis = new FileInputStream(filename); fis.read(buf, 0, buf.length);/*from ww w .java 2s . c o m*/ CRC32 crc = new CRC32(); ZipOutputStream s = new ZipOutputStream((OutputStream) new FileOutputStream(zipfilename)); s.setLevel(6); ZipEntry entry = new ZipEntry(filename); entry.setSize((long) buf.length); entry.setMethod(ZipEntry.DEFLATED); crc.reset(); crc.update(buf); entry.setCrc(crc.getValue()); s.putNextEntry(entry); s.write(buf, 0, buf.length); s.finish(); s.close(); }
From source file:Main.java
public void doZip(String filename, String zipfilename) throws Exception { byte[] buf = new byte[1024]; FileInputStream fis = new FileInputStream(filename); fis.read(buf, 0, buf.length);/*from www .j a v a2s . c om*/ CRC32 crc = new CRC32(); ZipOutputStream s = new ZipOutputStream((OutputStream) new FileOutputStream(zipfilename)); s.setLevel(6); ZipEntry entry = new ZipEntry(filename); entry.setSize((long) buf.length); entry.setTime(new Date().getTime()); crc.reset(); crc.update(buf); entry.setCrc(crc.getValue()); s.putNextEntry(entry); s.write(buf, 0, buf.length); s.finish(); s.close(); }
From source file:com.ftb2om2.util.Zipper.java
private void addToZip(String path, String name, ZipOutputStream zos) throws IOException { File file = new File(path); FileInputStream fis = new FileInputStream(file); ZipEntry zipEntry = new ZipEntry(name); zos.putNextEntry(zipEntry);/*from w w w . j av a2s . co m*/ byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { zos.write(bytes, 0, length); } zos.closeEntry(); fis.close(); }
From source file:ZipHandler.java
/** * makes a zip file named <xmlFileName> from <xmlURL> at path <zipURL> * @param zipURL/*from w ww .ja v a 2 s . co m*/ * @param xmlURL * @param xmlFileName */ public static void makeZip(String zipURL, String xmlURL, String xmlFileName) throws IOException { FileOutputStream fos = new FileOutputStream(new File(zipURL)); ZipOutputStream zos = new ZipOutputStream(fos); // zos.setLevel(); FileInputStream fis = new FileInputStream(xmlURL); zos.putNextEntry(new ZipEntry(xmlFileName + ".xml")); writeInOutputStream(fis, zos); String bpath = "c:\\"; FileInputStream fisBLOB = createInputStream(bpath); zos.putNextEntry(new ZipEntry("blob.lob")); writeInOutputStream(fisBLOB, zos); zos.closeEntry(); String cpath = "c:\\"; FileInputStream fisCLOB = createInputStream(cpath); zos.putNextEntry(new ZipEntry("clob.lob")); writeInOutputStream(fisCLOB, zos); zos.closeEntry(); fis.close(); fisCLOB.close(); fisBLOB.close(); zos.close(); fos.close(); }
From source file:Main.java
static private void addFileToZip(String path, File srcFile, ZipOutputStream zip, String destZipFile) throws Exception { if (srcFile.isDirectory()) { addFolderToZip(path, srcFile, zip, destZipFile); } else if (!srcFile.getName().equals(destZipFile)) { byte[] buf = new byte[1024]; int len;/* ww w. j ava 2 s . c om*/ final InputStream in = new BufferedInputStream(new FileInputStream(srcFile)); try { if (path.equals("/")) { zip.putNextEntry(new ZipEntry(srcFile.getName())); } else { zip.putNextEntry(new ZipEntry(path + "/" + srcFile.getName())); } while ((len = in.read(buf)) > 0) { zip.write(buf, 0, len); } } finally { in.close(); } } }
From source file:com.wisemapping.util.ZipUtils.java
public static byte[] bytesToZip(@NotNull final byte[] content) throws IOException { ZipOutputStream zip = null;//ww w . j ava 2 s . c o m final ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); try { zip = new ZipOutputStream(byteArray); ZipEntry zEntry = new ZipEntry("content"); zip.putNextEntry(zEntry); IOUtils.write(content, zip); zip.closeEntry(); } finally { if (zip != null) { zip.flush(); zip.close(); } } return byteArray.toByteArray(); }
From source file:Main.java
final static private boolean createZipFile(String out, String... in) { InputStream is = null;/*from ww w . j a v a2 s .c o m*/ ZipOutputStream zos = null; try { zos = new ZipOutputStream(new FileOutputStream(out)); } catch (FileNotFoundException e2) { e2.printStackTrace(); } try { for (int i = 0; i < in.length; i++) { is = new FileInputStream(in[i]); ZipEntry ze = new ZipEntry(in[i]); zos.putNextEntry(ze); int len = 0; byte[] buf = new byte[1024]; while ((len = is.read(buf)) != -1) { zos.write(buf, 0, len); } is.close(); zos.closeEntry(); } zos.close(); } catch (IOException e) { e.printStackTrace(); return false; } return true; }
From source file:com.samczsun.helios.utils.Utils.java
public static void save(File dest, Map<String, byte[]> data, Predicate<String> accept) { try {/*w ww . j av a 2 s .c o m*/ JarOutputStream out = new JarOutputStream(new FileOutputStream(dest)); Set<String> added = new HashSet<>(); for (Entry<String, byte[]> entry : data.entrySet()) { String name = entry.getKey(); if (added.add(name) && accept.test(name)) { out.putNextEntry(new ZipEntry(name)); out.write(entry.getValue()); out.closeEntry(); } } out.close(); } catch (IOException e) { ExceptionHandler.handle(e); } }
From source file:Main.java
static void zipDir(File zipDir, ZipOutputStream zos, String name) throws IOException { // Create a new File object based on the directory we have to zip if (name.endsWith(File.separator)) name = name.substring(0, name.length() - File.separator.length()); if (!name.endsWith(ZIP_FILE_SEPARATOR)) name = name + ZIP_FILE_SEPARATOR; // Place the zip entry in the ZipOutputStream object // Get a listing of the directory content File[] dirList = zipDir.listFiles(); if (dirList.length == 0) { // empty directory if (DEBUG) System.out.println("Add empty entry for directory : " + name); ZipEntry anEntry = new ZipEntry(name); zos.putNextEntry(anEntry);/*from w ww .jav a2s.com*/ return; } // Loop through dirList, and zip the files for (int i = 0; i < dirList.length; i++) { File f = dirList[i]; String fName = name + f.getName(); if (f.isDirectory()) { // if the File object is a directory, call this // function again to add its content recursively zipDir(f, zos, fName); } else { zipFile(f, zos, fName); } } return; }
From source file:ZipHelper.java
private static void fileToZip(File file, ZipOutputStream zout, File baseDir) throws Exception { String entryName = file.getPath().substring(baseDir.getPath().length() + 1); if (File.separatorChar != '/') entryName = entryName.replace(File.separator, "/"); if (file.isDirectory()) { zout.putNextEntry(new ZipEntry(entryName + "/")); zout.closeEntry();// w w w.j a va 2 s .c o m File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) fileToZip(files[i], zout, baseDir); } else { FileInputStream is = null; try { is = new FileInputStream(file); zout.putNextEntry(new ZipEntry(entryName)); streamCopy(is, zout); } finally { zout.closeEntry(); if (is != null) is.close(); } } }