List of usage examples for java.util.zip ZipOutputStream close
public void close() throws IOException
From source file:de.hybris.platform.impex.jalo.ImpExMediasImportTest.java
/** * Calls the <code>importData</code> method of given handler with an zip-file path in different formats. * //from w w w.ja va2 s . com * @param handler * handler which will be used for test * @param media * media where the data will be imported to */ private void mediaImportFromZip(final MediaDataHandler handler, final Media media) { File testFile = null; try { testFile = File.createTempFile("mediaImportTest", ".zip"); final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(testFile)); zos.putNextEntry(new ZipEntry(new File("files", "dummy.txt").getPath())); zos.putNextEntry(new ZipEntry(new File("files", "test.txt").getPath())); final PrintWriter printer = new PrintWriter(zos); printer.print("testest"); printer.flush(); zos.flush(); printer.close(); zos.close(); } catch (final IOException e) { e.printStackTrace(); fail(e.getMessage()); } final String unixPathRel = ImpExConstants.Syntax.ZIP_BASED_FILE_PATH_PREFIX + FilenameUtils.separatorsToUnix(testFile.getPath()) + "&files/test.txt"; final String unixPathAbs = ImpExConstants.Syntax.ZIP_BASED_FILE_PATH_PREFIX + FilenameUtils.separatorsToUnix(testFile.getAbsolutePath()) + "&files/test.txt"; final String winPathRel = ImpExConstants.Syntax.ZIP_BASED_FILE_PATH_PREFIX + FilenameUtils.separatorsToWindows(testFile.getPath()) + "&files\\test.txt"; final String winPathAbs = ImpExConstants.Syntax.ZIP_BASED_FILE_PATH_PREFIX + FilenameUtils.separatorsToWindows(testFile.getAbsolutePath()) + "&files\\test.txt"; try { mediaImport(handler, media, unixPathRel, "testest"); mediaImport(handler, media, unixPathAbs, "testest"); mediaImport(handler, media, winPathRel, "testest"); mediaImport(handler, media, winPathAbs, "testest"); } catch (final Exception e) { fail(e.getMessage()); } if (!testFile.delete()) { fail("Can not delete temp file: " + testFile.getPath()); } }
From source file:com.jd.survey.web.reports.ReportController.java
@Secured({ "ROLE_ADMIN", "ROLE_SURVEY_ADMIN" }) @RequestMapping(value = "/{id}", params = "spss", produces = "text/html") public void surveySPSSExport(@PathVariable("id") Long surveyDefinitionId, Principal principal, HttpServletRequest httpServletRequest, HttpServletResponse response) { try {/* w ww .ja va2 s . c om*/ User user = userService.user_findByLogin(principal.getName()); if (!securityService.userIsAuthorizedToManageSurvey(surveyDefinitionId, user)) { log.warn("Unauthorized access to url path " + httpServletRequest.getPathInfo() + " attempted by user login:" + principal.getName() + "from IP:" + httpServletRequest.getLocalAddr()); response.sendRedirect("../accessDenied"); //throw new AccessDeniedException("Unauthorized access attempt"); } String metadataFileName = "survey" + surveyDefinitionId + ".sps"; String dataFileName = "survey" + surveyDefinitionId + ".dat"; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zipfile = new ZipOutputStream(baos); //metadata zipfile.putNextEntry(new ZipEntry(metadataFileName)); zipfile.write(sPSSHelperService.getSurveyDefinitionSPSSMetadata(surveyDefinitionId, dataFileName)); //data zipfile.putNextEntry(new ZipEntry(dataFileName)); zipfile.write(sPSSHelperService.getSurveyDefinitionSPSSData(surveyDefinitionId)); zipfile.close(); //response.setContentType("text/html; charset=utf-8"); response.setContentType("application/octet-stream"); // Set standard HTTP/1.1 no-cache headers. response.setHeader("Cache-Control", "no-store, no-cache,must-revalidate"); // Set IE extended HTTP/1.1 no-cache headers (use addHeader). response.addHeader("Cache-Control", "post-check=0, pre-check=0"); // Set standard HTTP/1.0 no-cache header. response.setHeader("Pragma", "no-cache"); response.setHeader("Content-Disposition", "inline;filename=survey" + surveyDefinitionId + "_spss.zip"); ServletOutputStream servletOutputStream = response.getOutputStream(); //servletOutputStream.write(stringBuilder.toString().getBytes("UTF-8")); servletOutputStream.write(baos.toByteArray()); servletOutputStream.flush(); } catch (Exception e) { log.error(e.getMessage(), e); throw new RuntimeException(e); } }
From source file:S3DataManagerTest.java
@Test public void testZipSourceOneDir() throws Exception { clearSourceDirectory();/*from www . jav a2s. co m*/ String buildSpecName = "Buildspec.yml"; File buildSpec = new File("/tmp/source/" + buildSpecName); String buildSpecContents = "yo\n"; FileUtils.write(buildSpec, buildSpecContents); File sourceDir = new File("/tmp/source/src"); sourceDir.mkdir(); String srcFileName = "/tmp/source/src/file.java"; File srcFile = new File(srcFileName); String srcFileContents = "int i = 1;"; FileUtils.write(srcFile, srcFileContents); ZipOutputStream out = new ZipOutputStream(new FileOutputStream("/tmp/source.zip")); S3DataManager dataManager = createDefaultSource(); dataManager.zipSource("/tmp/source/", out, "/tmp/source/"); out.close(); File zip = new File("/tmp/source.zip"); assertTrue(zip.exists()); File unzipFolder = new File("/tmp/folder/"); unzipFolder.mkdir(); ZipFile z = new ZipFile(zip.getPath()); z.extractAll(unzipFolder.getPath()); String[] fileList = unzipFolder.list(); assertNotNull(fileList); Arrays.sort(fileList); assertTrue(fileList.length == 2); assertEquals(fileList[0], buildSpecName); File extractedBuildSpec = new File(unzipFolder.getPath() + "/" + buildSpecName); assertEquals(FileUtils.readFileToString(extractedBuildSpec), buildSpecContents); String s = fileList[1]; assertEquals(fileList[1], "src"); File extractedSrcFile = new File(unzipFolder.getPath() + "/src/file.java"); assertTrue(extractedSrcFile.exists()); assertEquals(FileUtils.readFileToString(extractedSrcFile), srcFileContents); }
From source file:com.espringtran.compressor4j.processor.Bzip2Processor.java
/** * Compress data// ww w .j a v a2s.c om * * @param fileCompressor * FileCompressor object * @return * @throws Exception */ @Override public byte[] compressData(FileCompressor fileCompressor) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); BZip2CompressorOutputStream cos = new BZip2CompressorOutputStream(baos); ZipOutputStream zos = new ZipOutputStream(cos); try { zos.setLevel(fileCompressor.getLevel().getValue()); zos.setMethod(ZipOutputStream.DEFLATED); zos.setComment(fileCompressor.getComment()); for (BinaryFile binaryFile : fileCompressor.getMapBinaryFile().values()) { zos.putNextEntry(new ZipEntry(binaryFile.getDesPath())); zos.write(binaryFile.getData()); zos.closeEntry(); } zos.flush(); zos.finish(); } catch (Exception e) { FileCompressor.LOGGER.error("Error on compress data", e); } finally { zos.close(); cos.close(); baos.close(); } return baos.toByteArray(); }
From source file:com.espringtran.compressor4j.processor.GzipProcessor.java
/** * Compress data//from w w w . j a v a 2 s .c om * * @param fileCompressor * FileCompressor object * @return * @throws Exception */ @Override public byte[] compressData(FileCompressor fileCompressor) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); GzipCompressorOutputStream cos = new GzipCompressorOutputStream(baos); ZipOutputStream zos = new ZipOutputStream(cos); try { zos.setLevel(fileCompressor.getLevel().getValue()); zos.setMethod(ZipOutputStream.DEFLATED); zos.setComment(fileCompressor.getComment()); for (BinaryFile binaryFile : fileCompressor.getMapBinaryFile().values()) { zos.putNextEntry(new ZipEntry(binaryFile.getDesPath())); zos.write(binaryFile.getData()); zos.closeEntry(); } zos.flush(); zos.finish(); } catch (Exception e) { FileCompressor.LOGGER.error("Error on compress data", e); } finally { zos.close(); cos.close(); baos.close(); } return baos.toByteArray(); }
From source file:fi.mikuz.boarder.util.FileProcessor.java
public void zipBoard(String boardName) { try {/*from ww w . ja va2 s .c o m*/ SoundboardMenu.mShareDir.mkdirs(); File inFolder = new File(SoundboardMenu.mSbDir, boardName); File outFile = new File(SoundboardMenu.mShareDir, boardName + ".zip"); mBoardDirLength = inFolder.getAbsolutePath().length() - boardName.length(); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFile)); System.out.println("Creating : " + outFile); zipAddDir(inFolder, out); out.close(); } catch (IOException e) { ACRA.getErrorReporter().handleException(e); Log.e(TAG, "Error zipping", e); } }
From source file:de.spiritcroc.ownlog.FileHelper.java
public static File generateExport(Context context, SQLiteDatabase db) { BufferedInputStream in = null; ZipOutputStream out = null; try {// ww w . jav a2 s. co m File outFile = getExportFile(context); outFile.delete(); outFile.createNewFile(); out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile))); File dbExportFile = getExportDbFile(context); PasswdHelper.cloneDb(db, dbExportFile); addFileToZip(out, dbExportFile, dbExportFile.getName()); // dbExportFile only temporary needed for zipping dbExportFile.delete(); File attachmentsPath = getAttachmentsBasePath(context); // Structure of attachment directory: entry/attachment String exportAttachmentsPath = getExportAttachmentsPath(context); for (File entry : attachmentsPath.listFiles()) { for (File file : entry.listFiles()) { addFileToZip(out, file, exportAttachmentsPath + File.separator + entry.getName() + File.separator + file.getName()); } } return outFile; } catch (IOException e) { e.printStackTrace(); return null; } finally { if (in != null) { try { in.close(); } catch (Exception e) { e.printStackTrace(); } } if (out != null) { try { out.close(); } catch (Exception e) { e.printStackTrace(); } } } }
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 www. j ava2 s. com 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(); }
From source file:S3DataManagerTest.java
@Test public void testZipSourceOneDirMultipleFiles() throws Exception { clearSourceDirectory();/*from w w w . j a va2 s . co m*/ String buildSpecName = "buildspec.yml"; String rootFileName = "pom.xml"; String sourceDirName = "src"; String srcFileName = "file.java"; String srcFile2Name = "util.java"; File buildSpec = new File("/tmp/source/" + buildSpecName); File rootFile = new File("/tmp/source/" + rootFileName); File sourceDir = new File("/tmp/source/" + sourceDirName); sourceDir.mkdir(); File srcFile = new File("/tmp/source/src/" + srcFileName); File srcFile2 = new File("/tmp/source/src/" + srcFile2Name); String rootFileContents = "<plugin>codebuild</plugin>"; String buildSpecContents = "Hello!!!!!"; String srcFileContents = "int i = 1;"; String srcFile2Contents = "util() { ; }"; FileUtils.write(buildSpec, buildSpecContents); FileUtils.write(rootFile, rootFileContents); FileUtils.write(srcFile, srcFileContents); FileUtils.write(srcFile2, srcFile2Contents); ZipOutputStream out = new ZipOutputStream(new FileOutputStream("/tmp/source.zip")); S3DataManager dataManager = createDefaultSource(); dataManager.zipSource("/tmp/source/", out, "/tmp/source/"); out.close(); File zip = new File("/tmp/source.zip"); assertTrue(zip.exists()); File unzipFolder = new File("/tmp/folder/"); unzipFolder.mkdir(); ZipFile z = new ZipFile(zip.getPath()); z.extractAll(unzipFolder.getPath()); assertTrue(unzipFolder.list().length == 3); File srcFolder = new File("/tmp/folder/src/"); assertTrue(srcFolder.list().length == 2); List<String> files = Arrays.asList(unzipFolder.list()); assertTrue(files.contains(buildSpecName)); assertTrue(files.contains(rootFileName)); assertTrue(files.contains(sourceDirName)); File extractedBuildSpec = new File(unzipFolder.getPath() + "/" + buildSpecName); File extractedRootFile = new File(unzipFolder.getPath() + "/" + rootFileName); File extractedSrcFile = new File(unzipFolder.getPath() + "/src/" + srcFileName); File extractedSrcFile2 = new File(unzipFolder.getPath() + "/src/" + srcFile2Name); assertTrue(FileUtils.readFileToString(extractedBuildSpec).equals(buildSpecContents)); assertTrue(FileUtils.readFileToString(extractedRootFile).equals(rootFileContents)); assertTrue(FileUtils.readFileToString(extractedSrcFile).equals(srcFileContents)); assertTrue(FileUtils.readFileToString(extractedSrcFile2).equals(srcFile2Contents)); }
From source file:com.mobicage.rogerthat.mfr.dal.Streamable.java
public String toBase64() { try {/*from w w w . ja v a 2s. c o m*/ ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { ZipOutputStream zip = new ZipOutputStream(bos); try { zip.setLevel(9); zip.putNextEntry(new ZipEntry("entry")); try { ObjectOutput out = new ObjectOutputStream(zip); try { out.writeObject(this); } finally { out.flush(); } } finally { zip.closeEntry(); } zip.flush(); return Base64.encodeBase64URLSafeString(bos.toByteArray()); } finally { zip.close(); } } finally { bos.close(); } } catch (IOException e) { log.log((Level.SEVERE), "IO Error while serializing member", e); return null; } }