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:nl.coinsweb.sdk.FileManager.java
private static void addFileToZip(String fromPath, Path zipPath, ZipOutputStream zos) { // Do dark magic, needed to correct ikvm consequences String pathInZip = zipPath.toFile().getPath().replace("\\", "/"); try {//w w w. j av a2 s.c om final byte[] buffer = new byte[1024]; ZipEntry ze = new ZipEntry(pathInZip); zos.putNextEntry(ze); log.trace("Adding to zip: " + pathInZip); // Write file content FileInputStream in = new FileInputStream(fromPath); int len; while ((len = in.read(buffer)) != -1) { zos.write(buffer, 0, len); } zos.closeEntry(); in.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:com.taobao.android.builder.tools.zip.ZipUtils.java
/** * ?zip?solib?/*from w w w .j av a 2s .c o m*/ * * @param output * @param srcDir * @throws Exception */ public static void addFileAndDirectoryToZip(File output, File srcDir) throws Exception { if (output.isDirectory()) { throw new IOException("This is a directory!"); } if (!output.getParentFile().exists()) { output.getParentFile().mkdirs(); } if (!output.exists()) { output.createNewFile(); } List fileList = getSubFiles(srcDir); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(output)); ZipEntry ze = null; byte[] buf = new byte[1024]; int readLen = 0; for (int i = 0; i < fileList.size(); i++) { File f = (File) fileList.get(i); ze = new ZipEntry(getAbsFileName(srcDir.getPath(), f)); ze.setSize(f.length()); ze.setTime(f.lastModified()); zos.putNextEntry(ze); InputStream is = new BufferedInputStream(new FileInputStream(f)); while ((readLen = is.read(buf, 0, 1024)) != -1) { zos.write(buf, 0, readLen); } is.close(); } zos.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);//w w w . ja va2 s .co 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); 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
private static void addFileToZip(String sourceFolderPath, String sourceFilePath, String baseFolderPath, ZipOutputStream zos) throws Exception { File item = new File(sourceFilePath); if (item == null || !item.exists()) return; //skip if the file is not exist if (isSymLink(item)) return; // do nothing to symbolic links. if (baseFolderPath == null) baseFolderPath = ""; if (item.isDirectory()) { for (String subItem : item.list()) { addFileToZip(sourceFolderPath + File.separator + item.getName(), sourceFilePath + File.separator + subItem, baseFolderPath, zos); }//from w ww. ja v a2 s . c o m } else { byte[] buf = new byte[102400]; //100k buffer int len; FileInputStream inStream = new FileInputStream(sourceFilePath); if (baseFolderPath.equals("")) //sourceFiles in absolute path, zip the file with absolute path zos.putNextEntry(new ZipEntry(sourceFilePath)); else {//relative path String relativePath = sourceFilePath.substring(baseFolderPath.length()); zos.putNextEntry(new ZipEntry(relativePath)); } while ((len = inStream.read(buf)) > 0) { zos.write(buf, 0, len); } } }
From source file:it.geosolutions.tools.compress.file.Compressor.java
public static File deflate(final File outputDir, final File zipFile, final File[] files, boolean overwrite) { if (zipFile.exists() && overwrite) { if (LOGGER.isInfoEnabled()) LOGGER.info("The output file already exists: " + zipFile + " overvriting"); return zipFile; }// ww w. j a va 2s . com // Create a buffer for reading the files byte[] buf = new byte[Conf.getBufferSize()]; ZipOutputStream out = null; BufferedOutputStream bos = null; FileOutputStream fos = null; try { fos = new FileOutputStream(zipFile); bos = new BufferedOutputStream(fos); out = new ZipOutputStream(bos); // Compress the files for (File file : files) { FileInputStream in = null; try { in = new FileInputStream(file); if (file.isDirectory()) { continue; } else { // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(file.getName())); // Transfer bytes from the file to the ZIP file int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.flush(); } } finally { try { // Complete the entry out.closeEntry(); } catch (Exception e) { } IOUtils.closeQuietly(in); } } } catch (IOException e) { LOGGER.error(e.getLocalizedMessage(), e); return null; } finally { // Complete the ZIP file IOUtils.closeQuietly(out); IOUtils.closeQuietly(bos); IOUtils.closeQuietly(fos); } return zipFile; }
From source file:it.geosolutions.tools.compress.file.Compressor.java
/** * This function zip the input directory. * /*from w w w.j a va2 s .com*/ * @param directory * The directory to be zipped. * @param base * The base directory. * @param out * The zip output stream. * @throws IOException */ public static void zipDirectory(final File directory, final File base, final ZipOutputStream out) throws IOException { if (directory != null && base != null && out != null) { File[] files = directory.listFiles(); byte[] buffer = new byte[4096]; int read = 0; FileInputStream in = null; ZipEntry entry = null; try { for (int i = 0, n = files.length; i < n; i++) { if (files[i].isDirectory()) { zipDirectory(files[i], base, out); } else { in = new FileInputStream(files[i]); entry = new ZipEntry(base.getName().concat("\\") .concat(files[i].getPath().substring(base.getPath().length() + 1))); out.putNextEntry(entry); while (-1 != (read = in.read(buffer))) { out.write(buffer, 0, read); } // ////////////////////// // Complete the entry // ////////////////////// out.closeEntry(); in.close(); in = null; } } } catch (IOException e) { if (LOGGER.isErrorEnabled()) LOGGER.error(e.getLocalizedMessage(), e); if (out != null) out.close(); } finally { if (in != null) in.close(); } } else throw new IOException("One or more input parameters are null!"); }
From source file:com.smash.revolance.ui.model.helper.ArchiveHelper.java
public static File buildArchive(File archive, File... files) throws FileNotFoundException { FileOutputStream fos = new FileOutputStream(archive); ZipOutputStream zos = new ZipOutputStream(fos); int bytesRead; byte[] buffer = new byte[1024]; CRC32 crc = new CRC32(); for (File file : files) { if (!file.exists()) { System.err.println("Skipping: " + file); continue; }// w ww . j a va 2 s .c o m BufferedInputStream bis = null; try { bis = new BufferedInputStream(new FileInputStream(file)); crc.reset(); while ((bytesRead = bis.read(buffer)) != -1) { crc.update(buffer, 0, bytesRead); } bis.close(); // Reset to beginning of input stream bis = new BufferedInputStream(new FileInputStream(file)); String entryPath = FileHelper.getRelativePath(archive.getParentFile(), file); ZipEntry entry = new ZipEntry(entryPath); entry.setMethod(ZipEntry.STORED); entry.setCompressedSize(file.length()); entry.setSize(file.length()); entry.setCrc(crc.getValue()); zos.putNextEntry(entry); while ((bytesRead = bis.read(buffer)) != -1) { zos.write(buffer, 0, bytesRead); } } catch (FileNotFoundException e) { } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } finally { IOUtils.closeQuietly(bis); } } IOUtils.closeQuietly(zos); return archive; }
From source file:edu.uci.ics.asterix.event.service.AsterixEventServiceUtil.java
private static void zipDir(File sourceDir, final File destFile, ZipOutputStream zos) throws IOException { File[] dirList = sourceDir.listFiles(new FileFilter() { public boolean accept(File f) { return !f.getName().endsWith(destFile.getName()); }// w w w. j av a2s. co m }); for (int i = 0; i < dirList.length; i++) { File f = dirList[i]; if (f.isDirectory()) { zipDir(f, destFile, zos); } else { int bytesIn = 0; byte[] readBuffer = new byte[2156]; FileInputStream fis = new FileInputStream(f); ZipEntry entry = new ZipEntry(sourceDir.getName() + File.separator + f.getName()); zos.putNextEntry(entry); while ((bytesIn = fis.read(readBuffer)) != -1) { zos.write(readBuffer, 0, bytesIn); } fis.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);//w w w . jav a2 s. 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);/*w ww . j a va 2 s . 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.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(); }