List of usage examples for java.util.zip ZipOutputStream putNextEntry
public void putNextEntry(ZipEntry e) throws IOException
From source file:herddb.upgrade.ZIPUtils.java
private static void addFileToZip(int skipprefix, File file, ZipOutputStream zipper) throws IOException { String raw = file.getAbsolutePath().replace("\\", "/"); if (raw.length() == skipprefix) { if (file.isDirectory()) { File[] listFiles = file.listFiles(); if (listFiles != null) { for (File child : listFiles) { addFileToZip(skipprefix, child, zipper); }/*from www . j ava2s . c o m*/ } } } else { String path = raw.substring(skipprefix + 1); if (file.isDirectory()) { ZipEntry entry = new ZipEntry(path); zipper.putNextEntry(entry); zipper.closeEntry(); File[] listFiles = file.listFiles(); if (listFiles != null) { for (File child : listFiles) { addFileToZip(skipprefix, child, zipper); } } } else { ZipEntry entry = new ZipEntry(path); zipper.putNextEntry(entry); try (FileInputStream in = new FileInputStream(file)) { IOUtils.copyLarge(in, zipper); } zipper.closeEntry(); } } }
From source file:Main.java
private static void zipFile(ZipOutputStream zipOutputStream, String sourcePath) throws IOException { java.io.File files = new java.io.File(sourcePath); java.io.File[] fileList = files.listFiles(); String entryPath = ""; BufferedInputStream input = null; for (java.io.File file : fileList) { if (file.isDirectory()) { zipFile(zipOutputStream, file.getPath()); } else {// w w w. j av a 2s . c o m byte data[] = new byte[BUFFER_SIZE]; FileInputStream fileInputStream = new FileInputStream(file.getPath()); input = new BufferedInputStream(fileInputStream, BUFFER_SIZE); entryPath = file.getAbsolutePath().replace(parentPath, ""); ZipEntry entry = new ZipEntry(entryPath); zipOutputStream.putNextEntry(entry); int count; while ((count = input.read(data, 0, BUFFER_SIZE)) != -1) { zipOutputStream.write(data, 0, count); } input.close(); } } }
From source file:FileUtil.java
/** * Zip up a directory path// w ww .jav a 2 s. c o m * @param directory * @param zos * @param path * @throws IOException */ public static void zipDir(String directory, ZipOutputStream zos, String path) throws IOException { File zipDir = new File(directory); // get a listing of the directory content String[] dirList = zipDir.list(); byte[] readBuffer = new byte[2156]; int bytesIn = 0; // loop through dirList, and zip the files for (int i = 0; i < dirList.length; i++) { File f = new File(zipDir, dirList[i]); if (f.isDirectory()) { String filePath = f.getPath(); zipDir(filePath, zos, path + f.getName() + "/"); continue; } FileInputStream fis = new FileInputStream(f); try { ZipEntry anEntry = new ZipEntry(path + f.getName()); zos.putNextEntry(anEntry); bytesIn = fis.read(readBuffer); while (bytesIn != -1) { zos.write(readBuffer, 0, bytesIn); bytesIn = fis.read(readBuffer); } } finally { fis.close(); } } }
From source file:apim.restful.importexport.utils.ArchiveGeneratorUtil.java
/** * Add files of the directory to the archive * * @param directoryToZip Location of the archive * @param file File to be included in the archive * @param zipOutputStream Output stream/*from w w w . j a v a2 s . c o m*/ * @throws APIExportException If an error occurs while writing files to the archive */ private static void addToArchive(File directoryToZip, File file, ZipOutputStream zipOutputStream) throws APIExportException { FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); // Get relative path from archive directory to the specific file String zipFilePath = file.getCanonicalPath().substring(directoryToZip.getCanonicalPath().length() + 1, file.getCanonicalPath().length()); if (File.separatorChar != '/') zipFilePath = zipFilePath.replace(File.separatorChar, APIImportExportConstants.ARCHIVE_PATH_SEPARATOR); ZipEntry zipEntry = new ZipEntry(zipFilePath); zipOutputStream.putNextEntry(zipEntry); IOUtils.copy(fileInputStream, zipOutputStream); zipOutputStream.closeEntry(); } catch (IOException e) { log.error("I/O error while writing files to archive" + e.getMessage()); throw new APIExportException("I/O error while writing files to archive", e); } finally { IOUtils.closeQuietly(fileInputStream); } }
From source file:com.taobao.android.builder.tools.classinject.CodeInjectByJavassist.java
private static void copyStreamToJar(InputStream zin, ZipOutputStream out, String currentName, long fileTime, ZipEntry zipEntry) throws IOException { // Create new entry for zip file. ZipEntry newEntry = new ZipEntry(currentName); // Make sure there is date and time set. if (fileTime != -1) { newEntry.setTime(fileTime); // If found set it into output file. }//ww w .j a v a2s. c o m out.putNextEntry(newEntry); if (zin != null) { IOUtils.copy(zin, out); } }
From source file:lucee.commons.io.compress.CompressUtil.java
private static void compressZip(String parent, Resource source, ZipOutputStream zos, ResourceFilter filter) throws IOException { if (source.isFile()) { //if(filter.accept(source)) { ZipEntry ze = new ZipEntry(parent); ze.setTime(source.lastModified()); zos.putNextEntry(ze); try {/*from ww w. j a v a 2 s .co m*/ IOUtil.copy(source, zos, false); } finally { zos.closeEntry(); } //} } else if (source.isDirectory()) { if (!StringUtil.isEmpty(parent)) { ZipEntry ze = new ZipEntry(parent + "/"); ze.setTime(source.lastModified()); try { zos.putNextEntry(ze); } catch (IOException ioe) { if (Caster.toString(ioe.getMessage()).indexOf("duplicate") == -1) throw ioe; } zos.closeEntry(); } compressZip(parent, filter == null ? source.listResources() : source.listResources(filter), zos, filter); } }
From source file:edu.umd.cs.submit.CommandLineSubmit.java
/** * @param p//from w w w . j a v a 2 s. c om * @param find * @param files * @param userProps * @return * @throws IOException * @throws FileNotFoundException */ public static MultipartPostMethod createFilePost(Properties p, FindAllFiles find, Collection<File> files, Properties userProps) throws IOException, FileNotFoundException { // ========================== assemble zip file in byte array // ============================== String loginName = userProps.getProperty("loginName"); String classAccount = userProps.getProperty("classAccount"); String from = classAccount; if (loginName != null && !loginName.equals(classAccount)) from += "/" + loginName; System.out.println(" submitted by " + from); System.out.println(); System.out.println("Submitting the following files"); ByteArrayOutputStream bytes = new ByteArrayOutputStream(4096); byte[] buf = new byte[4096]; ZipOutputStream zipfile = new ZipOutputStream(bytes); zipfile.setComment("zipfile for CommandLineTurnin, version " + VERSION); for (File resource : files) { if (resource.isDirectory()) continue; String relativePath = resource.getCanonicalPath().substring(find.rootPathLength + 1); System.out.println(relativePath); ZipEntry entry = new ZipEntry(relativePath); entry.setTime(resource.lastModified()); zipfile.putNextEntry(entry); InputStream in = new FileInputStream(resource); try { while (true) { int n = in.read(buf); if (n < 0) break; zipfile.write(buf, 0, n); } } finally { in.close(); } zipfile.closeEntry(); } // for each file zipfile.close(); MultipartPostMethod filePost = new MultipartPostMethod(p.getProperty("submitURL")); p.putAll(userProps); // add properties for (Map.Entry<?, ?> e : p.entrySet()) { String key = (String) e.getKey(); String value = (String) e.getValue(); if (!key.equals("submitURL")) filePost.addParameter(key, value); } filePost.addParameter("submitClientTool", "CommandLineTool"); filePost.addParameter("submitClientVersion", VERSION); byte[] allInput = bytes.toByteArray(); filePost.addPart(new FilePart("submittedFiles", new ByteArrayPartSource("submit.zip", allInput))); return filePost; }
From source file:com.dreikraft.axbo.sound.SoundPackageUtil.java
/** * saves a sound package with all meta information and audio files to a ZIP * file and creates the security tokens. * * @param packageFile the zip file, where the soundpackage should be stored * @param soundPackage the sound package info * @throws com.dreikraft.infactory.sound.SoundPackageException encapsulates * all low level (IO) exceptions/*from w ww . j a va 2 s . com*/ */ public static void exportSoundPackage(final File packageFile, final SoundPackage soundPackage) throws SoundPackageException { if (packageFile == null) { throw new SoundPackageException(new IllegalArgumentException("null package file")); } if (packageFile.delete()) { log.info("successfully deleted file: " + packageFile.getAbsolutePath()); } ZipOutputStream out = null; InputStream in = null; try { out = new ZipOutputStream(new FileOutputStream(packageFile)); out.setLevel(9); // write package info writePackageInfoZipEntry(soundPackage, out); // create path entries ZipEntry soundDir = new ZipEntry(SOUNDS_PATH_PREFIX + SL); out.putNextEntry(soundDir); out.flush(); out.closeEntry(); // write files for (Sound sound : soundPackage.getSounds()) { File axboFile = new File(sound.getAxboFile().getPath()); in = new FileInputStream(axboFile); writeZipEntry(SOUNDS_PATH_PREFIX + SL + axboFile.getName(), out, in); in.close(); } } catch (FileNotFoundException ex) { throw new SoundPackageException(ex); } catch (IOException ex) { throw new SoundPackageException(ex); } finally { if (out != null) { try { out.close(); } catch (IOException ex) { log.error("failed to close ZipOutputStream", ex); } } try { if (in != null) in.close(); } catch (IOException ex) { log.error("failed to close FileInputStream", ex); } } }
From source file:org.envirocar.app.util.Util.java
/** * Zips a list of files into the target archive. * /* w ww. j a v a 2 s . co m*/ * @param files * the list of files of the target archive * @param target * the target filename * @throws IOException */ public static void zipNative(List<File> files, String target) throws IOException { ZipOutputStream zos = null; try { File targetFile = new File(target); FileOutputStream dest = new FileOutputStream(targetFile); zos = new ZipOutputStream(new BufferedOutputStream(dest)); for (File f : files) { byte[] bytes = readFileContents(f).toByteArray(); ZipEntry entry = new ZipEntry(f.getName()); zos.putNextEntry(entry); zos.write(bytes); zos.closeEntry(); } } catch (IOException e) { throw e; } finally { try { if (zos != null) zos.close(); } catch (IOException e) { logger.warn(e.getMessage(), e); } } }
From source file:fll.web.GatherBugReport.java
/** * Add the web application and tomcat logs to the zipfile. */// www . j a v a2 s . c o m private static void addLogFiles(final ZipOutputStream zipOut, final ServletContext application) throws IOException { // get logs from the webapp final File fllAppDir = new File(application.getRealPath("/")); final File[] webLogs = fllAppDir.listFiles(new FilenameFilter() { @Override public boolean accept(final File dir, final String name) { return name.startsWith("fllweb.log"); } }); if (null != webLogs) { for (final File f : webLogs) { if (f.isFile()) { FileInputStream fis = null; try { zipOut.putNextEntry(new ZipEntry(f.getName())); fis = new FileInputStream(f); IOUtils.copy(fis, zipOut); fis.close(); } finally { IOUtils.closeQuietly(fis); } } } } // get tomcat logs final File webappsDir = fllAppDir.getParentFile(); LOGGER.trace("Webapps dir: " + webappsDir.getAbsolutePath()); final File tomcatDir = webappsDir.getParentFile(); LOGGER.trace("Tomcat dir: " + tomcatDir.getAbsolutePath()); final File tomcatLogDir = new File(tomcatDir, "logs"); LOGGER.trace("tomcat log dir: " + tomcatLogDir.getAbsolutePath()); final File[] tomcatLogs = tomcatLogDir.listFiles(); if (null != tomcatLogs) { for (final File f : tomcatLogs) { if (f.isFile()) { FileInputStream fis = null; try { zipOut.putNextEntry(new ZipEntry(f.getName())); fis = new FileInputStream(f); IOUtils.copy(fis, zipOut); fis.close(); } finally { IOUtils.closeQuietly(fis); } } } } }