List of usage examples for java.util.zip ZipEntry ZipEntry
public ZipEntry(ZipEntry e)
From source file:de.jwi.zip.Zipper.java
public void zip(ZipOutputStream out, File f, File base) throws IOException { String name = f.getPath().replace('\\', '/'); if (base != null) { String basename = base.getPath().replace('\\', '/'); if (name.startsWith(basename)) { name = name.substring(basename.length()); }// w w w . java 2s.c o m } if (name.startsWith("/")) { name = name.substring(1); } ZipEntry entry = new ZipEntry(name); entry.setTime(f.lastModified()); out.putNextEntry(entry); FileInputStream is = new FileInputStream(f); byte[] buf = new byte[BUFSIZE]; try { int l; while ((l = is.read(buf)) > -1) { out.write(buf, 0, l); } } finally { is.close(); } out.closeEntry(); }
From source file:com.chinamobile.bcbsp.fault.tools.Zip.java
/** * Compress files to *.zip./*from w w w.j av a 2s . com*/ * @param fileName * file name to compress * @return compressed file. */ public static String compress(String fileName) { String targetFile = null; File sourceFile = new File(fileName); Vector<File> vector = getAllFiles(sourceFile); try { if (sourceFile.isDirectory()) { targetFile = fileName + ".zip"; } else { char ch = '.'; targetFile = fileName.substring(0, fileName.lastIndexOf(ch)) + ".zip"; } BufferedInputStream bis = null; BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile)); ZipOutputStream zipos = new ZipOutputStream(bos); byte[] data = new byte[BUFFER]; if (vector.size() > 0) { for (int i = 0; i < vector.size(); i++) { File file = vector.get(i); zipos.putNextEntry(new ZipEntry(getEntryName(fileName, file))); bis = new BufferedInputStream(new FileInputStream(file)); int count; while ((count = bis.read(data, 0, BUFFER)) != -1) { zipos.write(data, 0, count); } bis.close(); zipos.closeEntry(); } zipos.close(); bos.close(); return targetFile; } else { File zipNullfile = new File(targetFile); zipNullfile.getParentFile().mkdirs(); zipNullfile.mkdir(); return zipNullfile.getAbsolutePath(); } } catch (IOException e) { LOG.error("[compress]", e); return "error"; } }
From source file:b2s.idea.mavenize.JarCombiner.java
private static void copyContentsOf(File input, ZipOutputStream output, JarContext context) throws IOException { ZipFile zip = null;/*w ww. java 2s .c o m*/ try { zip = new ZipFile(input); Enumeration<? extends ZipEntry> entries = zip.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (fileCanBeIgnored(entry, context)) { continue; } if (isServiceEntry(entry)) { context.addService(entry.getName(), contentsOf(entry, zip)); continue; } output.putNextEntry(new ZipEntry(entry.getName())); output.write(contentsOf(entry, zip)); output.flush(); output.closeEntry(); context.addVisitedEntry(entry); } } finally { close(zip); } }
From source file:com.jaspersoft.jasperserver.jrsh.common.ZipUtil.java
protected static void addFiles(ZipOutputStream zos, String folder, String baseFolder) throws Exception { File file = new File(folder); if (file.exists()) { if (file.isDirectory()) { if (!folder.equalsIgnoreCase(baseFolder)) { String entryName = folder.substring(baseFolder.length() + 1, folder.length()) + separatorChar; ZipEntry zipEntry = new ZipEntry(entryName); zos.putNextEntry(zipEntry); }/* w w w. ja va 2 s. c o m*/ File files[] = file.listFiles(); if (files != null) { for (File f : files) { addFiles(zos, f.getAbsolutePath(), baseFolder); } } } else { String entryName = folder.substring(baseFolder.length() + 1, folder.length()); ZipEntry zipEntry = new ZipEntry(entryName); zos.putNextEntry(zipEntry); try (FileInputStream in = new FileInputStream(folder)) { int len; byte buf[] = new byte[1024]; while ((len = in.read(buf)) > 0) { zos.write(buf, 0, len); } zos.closeEntry(); } } } }
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;// ww w . j a va 2 s .c om 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:com.recomdata.transmart.data.export.util.ZipUtil.java
/** * This method will bundle all the files into a zip file. * If there are 2 files with the same name, only the first file is part of the zip. * /* w ww . ja v a 2s. c om*/ * @param zipFileName * @param files * * @return zipFile absolute path * */ public static String bundleZipFile(String zipFileName, List<File> files) { File zipFile = null; Map<String, File> filesMap = new HashMap<String, File>(); if (StringUtils.isEmpty(zipFileName)) return null; try { zipFile = new File(zipFileName); if (zipFile.exists() && zipFile.isFile() && zipFile.delete()) { zipFile = new File(zipFileName); } ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile)); zipOut.setLevel(ZipOutputStream.DEFLATED); byte[] buffer = new byte[BUFFER_SIZE]; for (File file : files) { if (filesMap.containsKey(file.getName())) { continue; } else if (file.exists() && file.canRead()) { filesMap.put(file.getName(), file); zipOut.putNextEntry(new ZipEntry(file.getName())); FileInputStream fis = new FileInputStream(file); int bytesRead; while ((bytesRead = fis.read(buffer)) != -1) { zipOut.write(buffer, 0, bytesRead); } zipOut.flush(); zipOut.closeEntry(); } } zipOut.finish(); zipOut.close(); } catch (IOException e) { //log.error("Error while creating Zip file"); } return (null != zipFile) ? zipFile.getAbsolutePath() : null; }
From source file:Zip.java
/** * Create a OutputStream on a given file, transparently compressing the data * to a Zip file whose name is the provided file path, with a ".zip" * extension added.// ww w .j ava 2 s . c om * * @param file the file (with no zip extension) * * @return a OutputStream on the zip entry */ public static OutputStream createOutputStream(File file) { try { String path = file.getCanonicalPath(); FileOutputStream fos = new FileOutputStream(path + ".zip"); ZipOutputStream zos = new ZipOutputStream(fos); ZipEntry ze = new ZipEntry(file.getName()); zos.putNextEntry(ze); return zos; } catch (FileNotFoundException ex) { System.err.println(ex.toString()); System.err.println(file + " not found"); } catch (Exception ex) { System.err.println(ex.toString()); } return null; }
From source file:ZipTransformTest.java
public void testByteArrayTransformer() throws IOException { final String name = "foo"; final byte[] contents = "bar".getBytes(); File file1 = File.createTempFile("temp", null); File file2 = File.createTempFile("temp", null); try {/*from w w w . j av a 2s .c o m*/ // Create the ZIP file ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file1)); try { zos.putNextEntry(new ZipEntry(name)); zos.write(contents); zos.closeEntry(); } finally { IOUtils.closeQuietly(zos); } // Transform the ZIP file ZipUtil.transformEntry(file1, name, new ByteArrayZipEntryTransformer() { protected byte[] transform(ZipEntry zipEntry, byte[] input) throws IOException { String s = new String(input); assertEquals(new String(contents), s); return s.toUpperCase().getBytes(); } }, file2); // Test the ZipUtil byte[] actual = ZipUtil.unpackEntry(file2, name); assertNotNull(actual); assertEquals(new String(contents).toUpperCase(), new String(actual)); } finally { FileUtils.deleteQuietly(file1); FileUtils.deleteQuietly(file2); } }
From source file:b2s.idea.mavenize.JarCombiner.java
private void addServiceEntries(ZipOutputStream zipOut, JarContext context) throws IOException { for (Map.Entry<String, StringBuilder> entry : context.getServices()) { zipOut.putNextEntry(new ZipEntry(entry.getKey())); zipOut.write(entry.getValue().toString().getBytes()); zipOut.closeEntry();/* ww w . ja va 2s . c om*/ } }
From source file:com.baasbox.db.async.ExportJob.java
@Override public void run() { FileOutputStream dest = null; ZipOutputStream zip = null;/*from w ww. j av a 2 s. co m*/ FileOutputStream tempJsonOS = null; FileInputStream in = null; try { //File f = new File(this.fileName); File f = File.createTempFile("export", ".temp"); dest = new FileOutputStream(f); zip = new ZipOutputStream(dest); File tmpJson = File.createTempFile("export", ".json"); tempJsonOS = new FileOutputStream(tmpJson); DbHelper.exportData(this.appcode, tempJsonOS); BaasBoxLogger.info(String.format("Writing %d bytes ", tmpJson.length())); tempJsonOS.close(); ZipEntry entry = new ZipEntry("export.json"); zip.putNextEntry(entry); in = new FileInputStream(tmpJson); final int BUFFER = BBConfiguration.getImportExportBufferSize(); byte buffer[] = new byte[BUFFER]; int length; while ((length = in.read(buffer)) > 0) { zip.write(buffer, 0, length); } zip.closeEntry(); in.close(); File manifest = File.createTempFile("manifest", ".txt"); FileUtils.writeStringToFile(manifest, BBInternalConstants.IMPORT_MANIFEST_VERSION_PREFIX + BBConfiguration.getApiVersion()); ZipEntry entryManifest = new ZipEntry("manifest.txt"); zip.putNextEntry(entryManifest); zip.write(FileUtils.readFileToByteArray(manifest)); zip.closeEntry(); tmpJson.delete(); manifest.delete(); File finaldestination = new File(this.fileName); FileUtils.moveFile(f, finaldestination); } catch (Exception e) { BaasBoxLogger.error(ExceptionUtils.getMessage(e)); } finally { try { if (zip != null) zip.close(); if (dest != null) dest.close(); if (tempJsonOS != null) tempJsonOS.close(); if (in != null) in.close(); } catch (Exception ioe) { BaasBoxLogger.error(ExceptionUtils.getMessage(ioe)); } } }