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:com.solidmaps.webapp.service.ApostilamentosServiceImpl.java
private void addToZip(ZipOutputStream zos, String file) { if (StringUtils.isBlank(file)) { return;/*w ww .j a v a 2 s . c o m*/ } byte[] buffer = new byte[1024]; try { ZipEntry ze = new ZipEntry(file); zos.putNextEntry(ze); FileInputStream in = new FileInputStream(FILE_PATH + file); int len; while ((len = in.read(buffer)) > 0) { zos.write(buffer, 0, len); } in.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:isl.FIMS.utils.Utils.java
public static void createZip(String zipFile, String sourceDirectory) { try {/*from w w w.j a v a2 s . c o m*/ // String zipFile = "C:/FileIO/zipdemo.zip"; // String sourceDirectory = "C:/examples"; //create byte buffer byte[] buffer = new byte[1024]; //create object of FileOutputStream FileOutputStream fout = new FileOutputStream(zipFile); //create object of ZipOutputStream from FileOutputStream ZipOutputStream zout = new ZipOutputStream(fout); //create File object from directory name File dir = new File(sourceDirectory); //check to see if this directory exists if (!dir.isDirectory()) { } else { File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) { if (files[i].isFile()) { //create object of FileInputStream for source file FileInputStream fin = new FileInputStream(files[i]); zout.putNextEntry(new ZipEntry(files[i].getName())); int length; while ((length = fin.read(buffer)) > 0) { zout.write(buffer, 0, length); } zout.closeEntry(); //close the InputStream fin.close(); } } } //close the ZipOutputStream zout.close(); } catch (IOException ioe) { } }
From source file:org.agnitas.util.ZipUtilities.java
/** * Compress a file or recursively compress all files of a folder. * /*from w ww . jav a 2s . c o m*/ * @param sourceFile * @param destinationZipFileSream * @throws IOException */ public static void addFileToOpenZipFileStream(File sourceFile, String relativeDirPath, ZipOutputStream destinationZipFileSream) throws IOException { BufferedInputStream bufferedFileInputStream = null; if (!sourceFile.exists()) throw new IOException("SourceFile does not exist"); if (destinationZipFileSream == null) throw new IOException("DestinationStream is not ready"); if (relativeDirPath == null || (!relativeDirPath.endsWith("/") && !relativeDirPath.endsWith("\\"))) throw new IOException("RelativeDirPath is invalid"); try { if (!sourceFile.isDirectory()) { ZipEntry entry = new ZipEntry(relativeDirPath + sourceFile.getName()); entry.setTime(sourceFile.lastModified()); destinationZipFileSream.putNextEntry(entry); bufferedFileInputStream = new BufferedInputStream(new FileInputStream(sourceFile)); byte[] bufferArray = new byte[1024]; int byteBufferFillLength = bufferedFileInputStream.read(bufferArray); while (byteBufferFillLength > -1) { destinationZipFileSream.write(bufferArray, 0, byteBufferFillLength); byteBufferFillLength = bufferedFileInputStream.read(bufferArray); } bufferedFileInputStream.close(); bufferedFileInputStream = null; destinationZipFileSream.flush(); destinationZipFileSream.closeEntry(); } else { for (File sourceSubFile : sourceFile.listFiles()) { addFileToOpenZipFileStream(sourceSubFile, relativeDirPath + sourceFile.getName() + File.separator, destinationZipFileSream); } } } catch (IOException e) { throw e; } finally { if (bufferedFileInputStream != null) { try { bufferedFileInputStream.close(); } catch (Exception e) { } bufferedFileInputStream = null; } } }
From source file:com.music.scheduled.BackupJob.java
private void zipBackup(String baseFileName) throws IOException { FileOutputStream fos = new FileOutputStream(baseFileName + ".zip"); ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos)); File entryFile = new File(baseFileName + ".sql"); FileInputStream fi = new FileInputStream(entryFile); InputStream origin = new BufferedInputStream(fi, ZIP_BUFFER); ZipEntry entry = new ZipEntry("data.sql"); zos.putNextEntry(entry);//from w w w. ja v a 2s. com int count; byte[] data = new byte[ZIP_BUFFER]; while ((count = origin.read(data, 0, ZIP_BUFFER)) != -1) { zos.write(data, 0, count); } origin.close(); zos.close(); entryFile.delete(); }
From source file:org.dataconservancy.packaging.tool.model.impl.PackageStateBuilderImpl.java
private void writeToZipOutputStream(ZipOutputStream outputStream, File fileToWrite) throws IOException { //Add packageToolMetadata to the ZipOutputStream //Write bagInfoFile to Package state file outputStream.putNextEntry(new ZipEntry(fileToWrite.getName())); FileInputStream fis = new FileInputStream(fileToWrite); byte[] buffer = new byte[1024]; int length;/*from w w w . j av a 2s. co m*/ while ((length = fis.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } outputStream.closeEntry(); // close the InputStream fis.close(); }
From source file:it.eng.spagobi.tools.scheduler.dispatcher.UniqueMailDocumentDispatchChannel.java
public static MimeBodyPart zipAttachment(String zipFileName, Map mailOptions, File tempFolder) { logger.debug("IN"); MimeBodyPart messageBodyPart = null; try {//from ww w . ja v a2 s . c o m String nameSuffix = mailOptions.get(NAME_SUFFIX) != null ? (String) mailOptions.get(NAME_SUFFIX) : ""; byte[] buffer = new byte[4096]; // Create a buffer for copying int bytesRead; // the zip String tempFolderPath = (String) mailOptions.get(TEMP_FOLDER_PATH); ByteArrayOutputStream bout = new ByteArrayOutputStream(); ZipOutputStream out = new ZipOutputStream(bout); logger.debug("File zip to write: " + tempFolderPath + File.separator + "zippedFile.zip"); //files to zip String[] entries = tempFolder.list(); for (int i = 0; i < entries.length; i++) { //File f = new File(tempFolder, entries[i]); File f = new File(tempFolder + File.separator + entries[i]); if (f.isDirectory()) continue;//Ignore directory logger.debug("insert file: " + f.getName()); FileInputStream in = new FileInputStream(f); // Stream to read file ZipEntry entry = new ZipEntry(f.getName()); // Make a ZipEntry out.putNextEntry(entry); // Store entry while ((bytesRead = in.read(buffer)) != -1) out.write(buffer, 0, bytesRead); in.close(); } out.close(); messageBodyPart = new MimeBodyPart(); DataSource source = new ByteArrayDataSource(bout.toByteArray(), "application/zip"); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(zipFileName + nameSuffix + ".zip"); } catch (Exception e) { logger.error("Error while creating the zip", e); return null; } logger.debug("OUT"); return messageBodyPart; }
From source file:org.carlspring.strongbox.artifact.generator.NugetPackageGenerator.java
private void createMetadata(String id, String version, ZipOutputStream zos) throws IOException { ZipEntry ze = new ZipEntry("package/services/metadata/core-properties/metadata.psmdcp"); zos.putNextEntry(ze);//w w w . j a va 2 s . c o m ByteArrayInputStream is = new ByteArrayInputStream(String.format(PSMDCP_CONTENT, id, version).getBytes()); byte[] buffer = new byte[4096]; int len; while ((len = is.read(buffer)) > 0) { zos.write(buffer, 0, len); } is.close(); zos.closeEntry(); }
From source file:com.navjagpal.fileshare.StreamingZipEntity.java
public void writeTo(OutputStream out) throws IOException { Cursor c = mContentResolver.query(FileSharingProvider.Files.CONTENT_URI, new String[] { FileSharingProvider.Files.Columns.DISPLAY_NAME, FileSharingProvider.Files.Columns._DATA }, FileSharingProvider.Files.Columns.FOLDER_ID + "=?", new String[] { mFolderId }, null); ZipOutputStream zipOut = new ZipOutputStream(out); byte[] buf = new byte[BUFFER_SIZE]; while (c.moveToNext()) { String filename = c.getString(c.getColumnIndex(FileSharingProvider.Files.Columns.DISPLAY_NAME)); String data = c.getString(c.getColumnIndex(FileSharingProvider.Files.Columns._DATA)); zipOut.putNextEntry(new ZipEntry(filename)); InputStream input = mContentResolver.openInputStream(Uri.parse(data)); int len;/* w w w . j a v a 2 s . c o m*/ while ((len = input.read(buf)) > 0) { zipOut.write(buf, 0, len); } zipOut.closeEntry(); input.close(); } zipOut.finish(); mFinished = true; }
From source file:edu.stanford.epad.plugins.qifpwrapper.QIFPHandler.java
public static File generateZipFile(List<File> files, String dirPath) { File dir_file = new File(dirPath); File zip_file = new File(dirPath + "/temp_" + (fileNum++) + ".zip"); int dir_l = dir_file.getAbsolutePath().length(); FileOutputStream fos = null;// w w w . j a v a2 s. co m try { fos = new FileOutputStream(zip_file); } catch (FileNotFoundException e1) { log.warning("File not found", e1); return null; } ZipOutputStream zipout = new ZipOutputStream(fos); zipout.setLevel(1); for (int i = 0; i < files.size(); i++) { File f = (File) files.get(i); if (f.canRead()) { log.info("Adding file: " + f.getAbsolutePath()); try { zipout.putNextEntry(new ZipEntry(f.getAbsolutePath().substring(dir_l + 1))); } catch (Exception e) { log.warning("Error adding to zip file", e); return null; } BufferedInputStream fr; try { fr = new BufferedInputStream(new FileInputStream(f)); byte buffer[] = new byte[0xffff]; int b; while ((b = fr.read(buffer)) != -1) zipout.write(buffer, 0, b); fr.close(); zipout.closeEntry(); } catch (Exception e) { log.warning("Error closing zip file", e); return null; } } } try { zipout.finish(); fos.flush(); } catch (IOException e) { e.printStackTrace(); return null; } log.info("file zipped and returning as " + zip_file.getAbsolutePath()); return zip_file; }
From source file:br.univali.celine.lms.utils.zip.Zip.java
public void zip(ArrayList<String> fileList, File destFile, int compressionLevel) throws Exception { if (destFile.exists()) throw new Exception("File " + destFile.getName() + " already exists!!"); int fileLength; byte[] buffer = new byte[4096]; FileOutputStream fos = new FileOutputStream(destFile); BufferedOutputStream bos = new BufferedOutputStream(fos); ZipOutputStream zipFile = new ZipOutputStream(bos); zipFile.setLevel(compressionLevel);/*from w ww. ja v a 2s .c o m*/ for (int i = 0; i < fileList.size(); i++) { FileInputStream fis = new FileInputStream(fileList.get(i)); BufferedInputStream bis = new BufferedInputStream(fis); ZipEntry ze = new ZipEntry(FilenameUtils.getName(fileList.get(i))); zipFile.putNextEntry(ze); while ((fileLength = bis.read(buffer, 0, 4096)) > 0) zipFile.write(buffer, 0, fileLength); zipFile.closeEntry(); bis.close(); } zipFile.close(); }