List of usage examples for java.util.zip ZipEntry ZipEntry
public ZipEntry(ZipEntry e)
From source file:Main.java
private static final void zip(File directory, File base, ZipOutputStream zos) throws IOException { File[] files = directory.listFiles(); byte[] buffer = new byte[8192]; int read = 0; for (int i = 0, n = files.length; i < n; i++) { if (files[i].isDirectory()) { zip(files[i], base, zos);//from ww w. j ava 2 s. c o m } else { FileInputStream in = new FileInputStream(files[i]); ZipEntry entry = new ZipEntry(files[i].getPath().substring(base.getPath().length() + 1)); zos.putNextEntry(entry); while (-1 != (read = in.read(buffer))) { zos.write(buffer, 0, read); } in.close(); } } }
From source file:Main.java
public static void zipChildren(File folder, String prefix, ZipOutputStream out) throws IOException { File[] files = folder.listFiles(); if (files == null) return;/*from ww w. jav a 2 s . co m*/ for (File file : files) { if (file.isFile()) { String name = prefix + file.getName(); ZipEntry entry = new ZipEntry(name); entry.setTime(file.lastModified()); out.putNextEntry(entry); loadFromFile(file, out); out.closeEntry(); } else if (file.isDirectory()) { zipChildren(file, prefix + file.getName() + "/", out); } } }
From source file:Main.java
static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws IOException { File folder = new File(srcFile); if (folder.isDirectory()) { addFolderToZip(path, srcFile, zip, false); } else {/*from www . jav a 2s. c o m*/ byte[] buf = new byte[1024]; int len; FileInputStream in = new FileInputStream(srcFile); try { zip.putNextEntry(new ZipEntry(path + "/" + folder.getName())); while ((len = in.read(buf)) > 0) { zip.write(buf, 0, len); } } finally { in.close(); } } }
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();//from w w w .j a va2s.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); }
From source file:Main.java
public static void zipMutiCompress(File[] srcfile, File destFile) { byte[] buf = new byte[BUFFERED_SIZE]; try {//from w w w .j a va 2s.c om ZipOutputStream out = new ZipOutputStream(new FileOutputStream(destFile)); if (null != srcfile && srcfile.length > 0) { for (int i = 0; i < srcfile.length; i++) { File file = srcfile[i]; if (null != file) { FileInputStream in = new FileInputStream(file); out.putNextEntry(new ZipEntry(file.getName())); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } } } out.close(); } catch (IOException e) { } }
From source file:Main.java
public static boolean compress(File file) { try {/*from w ww .j a v a2 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 ww w . j a v a 2 s. co 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:Main.java
static void addDir(File dirObj, ZipOutputStream out) throws IOException { File[] files = dirObj.listFiles(); byte[] tmpBuf = new byte[1024]; for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { addDir(files[i], out);/*from www . j av a 2 s.c o m*/ continue; } FileInputStream in = new FileInputStream(files[i].getAbsolutePath()); System.out.println(" Adding: " + files[i].getAbsolutePath()); out.putNextEntry(new ZipEntry(files[i].getAbsolutePath())); int len; while ((len = in.read(tmpBuf)) > 0) { out.write(tmpBuf, 0, len); } out.closeEntry(); in.close(); } }
From source file:Utils.java
/** * Zip a list of file into one zip file. * /*from w ww.j a va 2s . co m*/ * @param files * files to zip * @param targetZipFile * target zip file * @throws IOException * IO error exception can be thrown when copying ... */ public static void zipFile(final File[] files, final File targetZipFile) throws IOException { try { FileOutputStream fos = new FileOutputStream(targetZipFile); ZipOutputStream zos = new ZipOutputStream(fos); byte[] buffer = new byte[128]; for (int i = 0; i < files.length; i++) { File currentFile = files[i]; if (!currentFile.isDirectory()) { ZipEntry entry = new ZipEntry(currentFile.getName()); FileInputStream fis = new FileInputStream(currentFile); zos.putNextEntry(entry); int read = 0; while ((read = fis.read(buffer)) != -1) { zos.write(buffer, 0, read); } zos.closeEntry(); fis.close(); } } zos.close(); fos.close(); } catch (FileNotFoundException e) { System.out.println("File not found : " + e); } }
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 {/* w ww . ja v a 2 s . co 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); } } }