List of usage examples for java.util.zip ZipOutputStream write
public synchronized void write(byte[] b, int off, int len) throws IOException
From source file:Main.java
public static boolean compress(File file) { try {/*from w ww . j av a 2 s. c o m*/ String fileName = file.getName(); if (fileName.indexOf(".") != -1) fileName = fileName.substring(0, fileName.indexOf(".")); FileOutputStream f = new FileOutputStream(file.getParent() + "/" + fileName + ".zip"); CheckedOutputStream cs = new CheckedOutputStream(f, new Adler32()); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs)); InputStream in = new FileInputStream(file); out.putNextEntry(new ZipEntry(file.getName())); int len = -1; byte buf[] = new byte[1024]; while ((len = in.read(buf, 0, 1024)) != -1) out.write(buf, 0, len); out.closeEntry(); in.close(); out.close(); return true; } catch (Exception e) { return false; } }
From source file:Main.java
/** * Compress a String to a zip file that has only one entry like zipName * The name shouldn't have .zip//from www . j a v a 2s .c o m * * @param content * @param fName * @throws IOException */ public static void zipAContentAsFileName(String fName, String content, String charset) throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(content.getBytes(charset)); BufferedInputStream bis = new BufferedInputStream(bais); File f = new File(fName); File parentFile = f.getParentFile(); if (!parentFile.exists()) { parentFile.mkdirs(); } ZipOutputStream zf = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(f + ".zip"))); ZipEntry entry = new ZipEntry(f.getName()); zf.putNextEntry(entry); byte[] barr = new byte[8192]; int len = 0; while ((len = bis.read(barr)) != -1) { zf.write(barr, 0, len); } zf.flush(); zf.close(); bis.close(); bais.close(); }
From source file:com.fredhopper.core.connector.index.FileUtils.java
public static void addToZipFile(final File file, final ZipOutputStream zos) throws IOException { final FileInputStream fis = new FileInputStream(file); final ZipEntry zipEntry = new ZipEntry(file.getName()); zos.putNextEntry(zipEntry);/*from w w w. j a v a 2 s . com*/ final 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:ch.rgw.compress.CompEx.java
public static byte[] CompressZIP(InputStream in) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[8192]; baos.write(buf, 0, 4); // Lnge des Originalstroms ZipOutputStream zo = new ZipOutputStream(baos); zo.putNextEntry(new ZipEntry("Data")); int l;/* ww w. j av a2 s. c om*/ long total = 0; ; while ((l = in.read(buf, 0, buf.length)) != -1) { zo.write(buf, 0, l); total += l; } zo.close(); byte[] ret = baos.toByteArray(); // Die hchstwertigen 3 Bit als Typmarker setzen total &= 0x1fffffff; total |= ZIP; BinConverter.intToByteArray((int) total, ret, 0); return ret; }
From source file:Main.java
public static void compressFiles(File files[], File fileCompressed) throws IOException { byte[] buffer = new byte[SIZE_OF_BUFFER]; ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(fileCompressed)); for (int i = 0; i < files.length; i++) { FileInputStream fileInputStream = new FileInputStream(files[i]); zipOutputStream.putNextEntry(new ZipEntry(files[i].getPath())); int size; while ((size = fileInputStream.read(buffer)) > 0) zipOutputStream.write(buffer, 0, size); zipOutputStream.closeEntry();/* w ww . j av a 2 s .c o m*/ fileInputStream.close(); } zipOutputStream.close(); }
From source file:com.formkiq.core.util.Zips.java
/** * Create Zip file.//w w w . j ava 2 s. com * @param map {@link Map} * @return byte[] zip file * @throws IOException IOException */ public static byte[] zipFile(final Map<String, byte[]> map) throws IOException { ByteArrayOutputStream bs = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(bs); for (Map.Entry<String, byte[]> e : map.entrySet()) { String name = e.getKey(); byte[] data = e.getValue(); ZipEntry ze = new ZipEntry(name); zip.putNextEntry(ze); zip.write(data, 0, data.length); zip.closeEntry(); } zip.finish(); byte[] bytes = bs.toByteArray(); IOUtils.closeQuietly(bs); IOUtils.closeQuietly(zip); return bytes; }
From source file:FolderZiper.java
static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception { File folder = new File(srcFile); if (folder.isDirectory()) { addFolderToZip(path, srcFile, zip); } else {//from ww w .jav a 2 s . c o m byte[] buf = new byte[1024]; int len; FileInputStream in = new FileInputStream(srcFile); zip.putNextEntry(new ZipEntry(path + "/" + folder.getName())); while ((len = in.read(buf)) > 0) { zip.write(buf, 0, len); } } }
From source file:com.splout.db.common.CompressorUtil.java
public static void createZip(File dir, File out, IOFileFilter filefilter, IOFileFilter dirFilter) throws IOException { Collection<File> files = FileUtils.listFiles(dir, filefilter, dirFilter); out.delete();/* w w w. ja v a2 s .c om*/ ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(out)); byte[] buf = new byte[1024]; for (File f : files) { ZipEntry ze = new ZipEntry(getRelativePath(f, dir)); zos.putNextEntry(ze); InputStream is = new FileInputStream(f); int cnt; while ((cnt = is.read(buf)) >= 0) { zos.write(buf, 0, cnt); } is.close(); zos.flush(); zos.closeEntry(); } zos.close(); }
From source file:Main.java
private static void compressFile(File file, ZipOutputStream out, String basedir) { if (!file.exists()) { return;//ww w . j a va 2 s . c o m } try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); ZipEntry entry = new ZipEntry(basedir + file.getName()); out.putNextEntry(entry); int count; byte data[] = new byte[BUFFER]; while ((count = bis.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } bis.close(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static void compressAFileToZip(File zip, File source) throws IOException { Log.d("source: " + source.getAbsolutePath(), ", length: " + source.length()); Log.d("zip:", zip.getAbsolutePath()); zip.getParentFile().mkdirs();/* ww w.j a v a2 s . com*/ File tempFile = new File(zip.getAbsolutePath() + ".tmp"); FileOutputStream fos = new FileOutputStream(tempFile); BufferedOutputStream bos = new BufferedOutputStream(fos); ZipOutputStream zos = new ZipOutputStream(bos); ZipEntry zipEntry = new ZipEntry(source.getName()); zipEntry.setTime(source.lastModified()); zos.putNextEntry(zipEntry); FileInputStream fis = new FileInputStream(source); BufferedInputStream bis = new BufferedInputStream(fis); byte[] bArr = new byte[4096]; int byteRead = 0; while ((byteRead = bis.read(bArr)) != -1) { zos.write(bArr, 0, byteRead); } zos.flush(); zos.closeEntry(); zos.close(); Log.d("zipEntry: " + zipEntry, "compressedSize: " + zipEntry.getCompressedSize()); bos.flush(); fos.flush(); bos.close(); fos.close(); bis.close(); fis.close(); zip.delete(); tempFile.renameTo(zip); }