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:io.apigee.buildTools.enterprise4g.utils.ZipUtils.java
static void addDir(File dirObj, ZipOutputStream out, File root, String prefix) 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, root, prefix); continue; }/* w w w. ja v a 2 s .co m*/ FileInputStream in = new FileInputStream(files[i].getAbsolutePath()); log.debug(" Adding: " + files[i].getAbsolutePath()); String relativePath = files[i].getCanonicalPath().substring(root.getCanonicalPath().length()); while (relativePath.startsWith("/")) { relativePath = relativePath.substring(1); } while (relativePath.startsWith("\\")) { relativePath = relativePath.substring(1); } String left = Matcher.quoteReplacement("\\"); String right = Matcher.quoteReplacement("/"); relativePath = relativePath.replaceAll(left, right); relativePath = (prefix == null) ? relativePath : prefix + "/" + relativePath; out.putNextEntry(new ZipEntry(relativePath)); int len; while ((len = in.read(tmpBuf)) > 0) { out.write(tmpBuf, 0, len); } out.closeEntry(); in.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);/* ww w . jav a 2 s . com*/ 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:com.jcalvopinam.core.Zipping.java
private static void splitAndZipFile(File inputFile, int bufferSize, CustomFile customFile) throws IOException { int counter = 1; byte[] bufferPart; byte[] buffer = new byte[bufferSize]; File newFile;/*from w w w .j av a 2s . c o m*/ FileInputStream fileInputStream; ZipOutputStream out; String temporalName; String outputFileName; try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(inputFile))) { int tmp; System.out.println("Please wait while the file is split:"); while ((tmp = bis.read(buffer)) > 0) { temporalName = String.format("%s.%03d", customFile.getFileName(), counter); newFile = new File(inputFile.getParent(), temporalName); try (FileOutputStream fileOutputStream = new FileOutputStream(newFile)) { fileOutputStream.write(buffer, 0, tmp); } fileInputStream = new FileInputStream(newFile);//file001.zip outputFileName = String.format("%s%s_%03d%s", customFile.getPath(), customFile.getFileName(), counter, Extensions.ZIP.getExtension()); out = new ZipOutputStream(new FileOutputStream(outputFileName)); out.putNextEntry(new ZipEntry(customFile.getFileNameExtension())); bufferPart = new byte[CustomFile.BYTE_SIZE]; int count; while ((count = fileInputStream.read(bufferPart)) > 0) { out.write(bufferPart, 0, count); System.out.print("."); } counter++; fileInputStream.close(); out.close(); FileUtils.deleteQuietly(newFile); } } System.out.println("\nEnded process!"); }
From source file:it.geosolutions.tools.compress.file.Compressor.java
/** * This function zip the input file./* w ww.j a va2 s.c om*/ * * @param file * The input file to be zipped. * @param out * The zip output stream. * @throws IOException */ public static void zipFile(final File file, final ZipOutputStream out) throws IOException { if (file != null && out != null) { // /////////////////////////////////////////// // Create a buffer for reading the files // /////////////////////////////////////////// byte[] buf = new byte[4096]; FileInputStream in = null; try { in = new FileInputStream(file); // ////////////////////////////////// // Add ZIP entry to output stream. // ////////////////////////////////// out.putNextEntry(new ZipEntry(FilenameUtils.getName(file.getAbsolutePath()))); // ////////////////////////////////////////////// // Transfer bytes from the file to the ZIP file // ////////////////////////////////////////////// int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // ////////////////////// // Complete the entry // ////////////////////// out.closeEntry(); } 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:org.agnitas.util.ZipUtilities.java
/** * Open an existing Zip file for adding new entries. * All existing entries in the zipped file will be copied in the new one. * //from ww w.ja va 2 s. c om * @param zipFile * @return * @throws IOException * @throws ZipException */ public static ZipOutputStream openExistingZipFileForExtension(File zipFile) throws IOException { // Rename source Zip file (Attention: the String path and name of the zipFile are preserved File originalFileTemp = new File( zipFile.getParentFile().getAbsolutePath() + "/" + String.valueOf(System.currentTimeMillis())); zipFile.renameTo(originalFileTemp); ZipFile sourceZipFile = new ZipFile(originalFileTemp); ZipOutputStream zipOutputStream = new ZipOutputStream( new BufferedOutputStream(new FileOutputStream(zipFile))); BufferedInputStream bufferedInputStream = null; try { // copy entries Enumeration<? extends ZipEntry> srcEntries = sourceZipFile.entries(); while (srcEntries.hasMoreElements()) { ZipEntry sourceZipFileEntry = srcEntries.nextElement(); zipOutputStream.putNextEntry(sourceZipFileEntry); bufferedInputStream = new BufferedInputStream(sourceZipFile.getInputStream(sourceZipFileEntry)); byte[] bufferArray = new byte[1024]; int byteBufferFillLength = bufferedInputStream.read(bufferArray); while (byteBufferFillLength > -1) { zipOutputStream.write(bufferArray, 0, byteBufferFillLength); byteBufferFillLength = bufferedInputStream.read(bufferArray); } zipOutputStream.closeEntry(); bufferedInputStream.close(); bufferedInputStream = null; } zipOutputStream.flush(); sourceZipFile.close(); originalFileTemp.delete(); return zipOutputStream; } catch (IOException e) { // delete existing Zip file if (zipFile.exists()) { if (zipOutputStream != null) { try { zipOutputStream.close(); } catch (Exception ex) { } zipOutputStream = null; } zipFile.delete(); } // revert renaming of source Zip file originalFileTemp.renameTo(zipFile); throw e; } finally { if (bufferedInputStream != null) { try { bufferedInputStream.close(); } catch (Exception e) { } bufferedInputStream = null; } } }
From source file:de.baumann.thema.RequestActivity.java
private static void zipFile(final String path, final ZipOutputStream out, final String relPath) throws IOException { final File file = new File(path); if (!file.exists()) { if (DEBUG) Log.d(TAG, file.getName() + " does NOT exist!"); return;/*w w w.j a v a2 s.co m*/ } final byte[] buf = new byte[1024]; final String[] files = file.list(); if (file.isFile()) { try (FileInputStream in = new FileInputStream(file.getAbsolutePath())) { out.putNextEntry(new ZipEntry(relPath + file.getName())); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } catch (ZipException zipE) { if (DEBUG) Log.d(TAG, zipE.getMessage()); } finally { if (out != null) out.closeEntry(); } } else if (files.length > 0) // non-empty folder { for (String file1 : files) { zipFile(path + "/" + file1, out, relPath + file.getName() + "/"); } } }
From source file:S3DataManager.java
public static void zipSource(final String directory, final ZipOutputStream out, final String prefixToTrim) throws Exception { if (!Paths.get(directory).startsWith(Paths.get(prefixToTrim))) { throw new Exception(zipSourceError + "prefixToTrim: " + prefixToTrim + ", directory: " + directory); }/*from w ww .j av a 2 s.co m*/ File dir = new File(directory); String[] dirFiles = dir.list(); if (dirFiles == null) { throw new Exception("Invalid directory path provided: " + directory); } byte[] buffer = new byte[1024]; int bytesRead; for (int i = 0; i < dirFiles.length; i++) { File f = new File(dir, dirFiles[i]); if (f.isDirectory()) { if (f.getName().equals(".git") == false) { zipSource(f.getPath() + File.separator, out, prefixToTrim); } } else { FileInputStream inputStream = new FileInputStream(f); try { String path = trimPrefix(f.getPath(), prefixToTrim); ZipEntry entry = new ZipEntry(path); out.putNextEntry(entry); while ((bytesRead = inputStream.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } } finally { inputStream.close(); } } } }
From source file:com.maxl.java.aips2xml.Aips2Xml.java
static void zipToFile(String dir_name, String file_name) { byte[] buffer = new byte[1024]; try {/*from w w w . j a va 2 s. co m*/ FileOutputStream fos = new FileOutputStream(dir_name + changeExtension(file_name, "zip")); ZipOutputStream zos = new ZipOutputStream(fos); ZipEntry ze = new ZipEntry(file_name); zos.putNextEntry(ze); FileInputStream in = new FileInputStream(dir_name + file_name); int len = 0; while ((len = in.read(buffer)) > 0) { zos.write(buffer, 0, len); } in.close(); zos.closeEntry(); zos.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:ch.silviowangler.dox.export.DoxExporterImpl.java
private void writeToZipOutputStream(ZipOutputStream out, byte[] dataBytes, String path) throws IOException { logger.trace("Writing to ZIP output stream using path '{}' and data length '{}'", path, dataBytes.length); out.putNextEntry(new ZipEntry(path)); out.write(dataBytes, 0, dataBytes.length); out.closeEntry(); // end of entry }
From source file:de.xwic.appkit.core.util.ZipUtil.java
/** * Zips the files array into a file that has the name given as parameter. * // w ww. j ava 2 s.c o m * @param files * the files array * @param zipFileName * the name for the zip file * @return the new zipped file * @throws IOException */ public static File zip(File[] files, String zipFileName) throws IOException { FileOutputStream stream = null; ZipOutputStream out = null; File archiveFile = null; try { if (!zipFileName.endsWith(".zip")) { zipFileName = zipFileName + ".zip"; } archiveFile = new File(zipFileName); byte buffer[] = new byte[BUFFER_SIZE]; // Open archive file stream = new FileOutputStream(archiveFile); out = new ZipOutputStream(stream); for (int i = 0; i < files.length; i++) { if (null == files[i] || !files[i].exists() || files[i].isDirectory()) { continue; } log.info("Zipping " + files[i].getName()); // Add archive entry ZipEntry zipAdd = new ZipEntry(files[i].getName()); zipAdd.setTime(files[i].lastModified()); out.putNextEntry(zipAdd); // Read input & write to output FileInputStream in = new FileInputStream(files[i]); while (true) { int nRead = in.read(buffer, 0, buffer.length); if (nRead <= 0) { break; } out.write(buffer, 0, nRead); } in.close(); } } catch (IOException e) { log.error("Error: " + e.getMessage(), e); throw e; } finally { try { if (null != out) { out.close(); } if (null != stream) { stream.close(); } } catch (IOException e) { log.error("Error: " + e.getMessage(), e); throw e; } } return archiveFile; }