List of usage examples for java.util.zip ZipOutputStream putNextEntry
public void putNextEntry(ZipEntry e) throws IOException
From source file:com.mosso.client.cloudfiles.sample.FilesCopy.java
/** * * @param folder/* ww w. j av a 2s . c o m*/ * @return null if the input is not a folder otherwise a zip file containing all the files in the folder with nested folders skipped. * @throws IOException */ public static File zipFolder(File folder) throws IOException { byte[] buf = new byte[1024]; int len; // Create the ZIP file String filenameWithZipExt = folder.getName() + ZIPEXTENSION; File zippedFile = new File(FilenameUtils.concat(SYSTEM_TMP.getAbsolutePath(), filenameWithZipExt)); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zippedFile)); if (folder.isDirectory()) { File[] files = folder.listFiles(); for (File f : files) { if (!f.isDirectory()) { FileInputStream in = new FileInputStream(f); // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(f.getName())); // Transfer bytes from the file to the ZIP file while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); in.close(); } else logger.warn("Skipping nested folder: " + f.getAbsoluteFile()); } out.flush(); out.close(); } else { logger.warn("The folder name supplied is not a folder!"); System.err.println("The folder name supplied is not a folder!"); return null; } return zippedFile; }
From source file:b2s.idea.mavenize.JarCombiner.java
private static void copyContentsOf(File input, ZipOutputStream output, JarContext context) throws IOException { ZipFile zip = null;/*w w w. ja v a 2 s. 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.mc.printer.model.utils.ZipHelper.java
private static void writeZip(File file, String parentPath, ZipOutputStream zos) { if (file.exists()) { if (file.isDirectory()) {//? parentPath += file.getName() + File.separator; File[] files = file.listFiles(); for (File f : files) { writeZip(f, parentPath, zos); }//from w w w .j a v a 2 s .com } else { FileInputStream fis = null; try { fis = new FileInputStream(file); ZipEntry ze = new ZipEntry(parentPath + file.getName()); zos.putNextEntry(ze); byte[] content = new byte[1024]; int len; while ((len = fis.read(content)) != -1) { zos.write(content, 0, len); zos.flush(); } } catch (FileNotFoundException e) { log.error("create zip file failed.", e); } catch (IOException e) { log.error("create zip file failed.", e); } finally { try { if (fis != null) { fis.close(); } } catch (IOException e) { log.error("create zip file failed.", e); } } } } }
From source file:com.izforge.izpack.util.IoHelper.java
public static void copyStreamToJar(InputStream zin, java.util.zip.ZipOutputStream out, String currentName, long fileTime) 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. }/* w w w.j ava 2s . c o m*/ out.putNextEntry(newEntry); if (zin != null) { IOUtils.copy(zin, out); } out.closeEntry(); }
From source file:Main.java
public static String zip(String filename) throws IOException { BufferedInputStream origin = null; Integer BUFFER_SIZE = 20480;/*w ww. j av a2 s.c o m*/ if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) { File flockedFilesFolder = new File( Environment.getExternalStorageDirectory() + File.separator + "FlockLoad"); System.out.println("FlockedFileDir: " + flockedFilesFolder); String uncompressedFile = flockedFilesFolder.toString() + "/" + filename; String compressedFile = flockedFilesFolder.toString() + "/" + "flockZip.zip"; ZipOutputStream out = new ZipOutputStream( new BufferedOutputStream(new FileOutputStream(compressedFile))); out.setLevel(9); try { byte data[] = new byte[BUFFER_SIZE]; FileInputStream fi = new FileInputStream(uncompressedFile); System.out.println("Filename: " + uncompressedFile); System.out.println("Zipfile: " + compressedFile); origin = new BufferedInputStream(fi, BUFFER_SIZE); try { ZipEntry entry = new ZipEntry( uncompressedFile.substring(uncompressedFile.lastIndexOf("/") + 1)); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) { out.write(data, 0, count); } } finally { origin.close(); } } finally { out.close(); } return "flockZip.zip"; } return null; }
From source file:gov.nih.nci.caintegrator.common.Cai2Util.java
private static void addFile(File curFile, ZipOutputStream out, int index) throws IOException { byte[] tmpBuf = new byte[BUFFER_SIZE]; FileInputStream in = new FileInputStream(curFile); String relativePathName = curFile.getPath().substring(index); out.putNextEntry(new ZipEntry(relativePathName)); int len;//w w w. ja v a 2s . c o m while ((len = in.read(tmpBuf)) > 0) { out.write(tmpBuf, 0, len); } // Complete the entry out.closeEntry(); in.close(); }
From source file:es.juntadeandalucia.panelGestion.negocio.utiles.Utils.java
public static byte[] compressXMLFiles(XMLFileVO[] xmlFiles) throws TransformerException, IOException { byte[] compressedFiles = null; ByteArrayOutputStream byteOS = new ByteArrayOutputStream(); ZipOutputStream zipOS = new ZipOutputStream(byteOS); for (XMLFileVO xmlFile : xmlFiles) { ZipEntry entry = new ZipEntry(xmlFile.getName()); zipOS.putNextEntry(entry); byte[] xml = documentToByte(xmlFile.getXml()); ByteArrayInputStream byteIS = new ByteArrayInputStream(xml); IOUtils.copy(byteIS, zipOS);/* w w w. j a va 2 s.co m*/ byteIS.close(); zipOS.closeEntry(); } zipOS.close(); compressedFiles = byteOS.toByteArray(); return compressedFiles; }
From source file:moskitt4me.repositoryClient.core.util.RepositoryClientUtil.java
public static void addFileToZip(File file, ZipOutputStream zos) throws Exception { zos.putNextEntry(new ZipEntry(file.getName())); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); long bytesRead = 0; byte[] bytesIn = new byte[1024]; int read = 0; while ((read = bis.read(bytesIn)) != -1) { zos.write(bytesIn, 0, read);//from ww w . j a v a2 s . c om bytesRead += read; } zos.closeEntry(); bis.close(); }
From source file:com.l2jfree.sql.L2DataSource.java
protected static final boolean writeBackup(String databaseName, InputStream in) throws IOException { FileUtils.forceMkdir(new File("backup/database")); final Date time = new Date(); final L2TextBuilder tb = new L2TextBuilder(); tb.append("backup/database/DatabaseBackup_"); tb.append(new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date())); tb.append("_uptime-").append(L2Config.getShortUptime()); tb.append(".zip"); final File backupFile = new File(tb.moveToString()); int written = 0; ZipOutputStream out = null; try {//from w ww .ja v a 2 s. c o m out = new ZipOutputStream(new FileOutputStream(backupFile)); out.setMethod(ZipOutputStream.DEFLATED); out.setLevel(Deflater.BEST_COMPRESSION); out.setComment("L2jFree Schema Backup Utility\r\n\r\nBackup date: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS z").format(new Date())); out.putNextEntry(new ZipEntry(databaseName + ".sql")); byte[] buf = new byte[4096]; for (int read; (read = in.read(buf)) != -1;) { out.write(buf, 0, read); written += read; } } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } if (written == 0) { backupFile.delete(); return false; } _log.info("DatabaseBackupManager: Database `" + databaseName + "` backed up successfully in " + (System.currentTimeMillis() - time.getTime()) / 1000 + " s."); return true; }
From source file:Main.java
static void zipDir(File zipDir, ZipOutputStream zos, String name) throws IOException { // Create a new File object based on the directory we have to zip if (name.endsWith(File.separator)) name = name.substring(0, name.length() - File.separator.length()); if (!name.endsWith(ZIP_FILE_SEPARATOR)) name = name + ZIP_FILE_SEPARATOR; // Place the zip entry in the ZipOutputStream object // Get a listing of the directory content File[] dirList = zipDir.listFiles(); if (dirList.length == 0) { // empty directory if (DEBUG) System.out.println("Add empty entry for directory : " + name); ZipEntry anEntry = new ZipEntry(name); zos.putNextEntry(anEntry); return;//from w ww. j ava 2s .c om } // Loop through dirList, and zip the files for (int i = 0; i < dirList.length; i++) { File f = dirList[i]; String fName = name + f.getName(); if (f.isDirectory()) { // if the File object is a directory, call this // function again to add its content recursively zipDir(f, zos, fName); } else { zipFile(f, zos, fName); } } return; }