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.netpace.aims.controller.application.WapApplicationHelper.java
public static boolean createWapImagesZipFile(File imagesZipFile, String imageFileName, Long wapApplicationID, Long allianceID) throws AimsException, HibernateException { byte[] buf = new byte[1024];// Create a buffer for reading the files boolean zipCreated = false; ZipOutputStream zipOut = null; InputStream imgInput = null;/*from w w w . ja v a2 s.c om*/ //general exception for zip creation AimsException aimsException = new AimsException("Error"); aimsException.addException(new AimsException("error.wap.app.zip.create")); String fileExtension = ""; String fName = ""; String[] imagesDirNames = new String[] { "img_174", "img_240", "img_320" }; //if images not found, then this statement throws aims exception AimsTempFile[] wapImageFiles = WapApplicationHelper.getImagesTempFiles(wapApplicationID, allianceID); try { zipOut = new ZipOutputStream(new FileOutputStream(imagesZipFile)); // Compress the files for (int i = 0; i < wapImageFiles.length; i++) { try { imgInput = wapImageFiles[i].getTempFile().getBinaryStream(); //get extension of filename in lower case fName = wapImageFiles[i].getTempFileName(); fileExtension = (fName.substring(fName.lastIndexOf("."))).toLowerCase(); // Add ZIP entry to output stream. dirName/filename+extension zipOut.putNextEntry(new ZipEntry(imagesDirNames[i] + "/" + (imageFileName + fileExtension))); // Transfer bytes from the file to the ZIP file int len; while ((len = imgInput.read(buf)) > 0) { zipOut.write(buf, 0, len); } } //end try catch (SQLException sqle) { System.out.println("Problem in getting image file as stream"); sqle.printStackTrace(); // throw aimsException; } catch (IOException e) { System.out.println("Problem in adding zip entry"); e.printStackTrace(); throw aimsException; } finally { // Complete the entry try { zipOut.closeEntry(); imgInput.close(); } catch (IOException ioe) { System.out.println("Error closing zip entry or input stream for image"); ioe.printStackTrace(); } } //end finally } //end for zipCreated = true; log.debug("wap images zip file created for ftp transfer: " + imagesZipFile.getName() + " in directory: " + imagesZipFile.getAbsolutePath()); } catch (FileNotFoundException e) { System.out.println(imagesZipFile + " not found in temp directory: " + imagesZipFile.getAbsolutePath()); e.printStackTrace();//zip file not found throw aimsException; } finally { try { if (zipOut != null) { zipOut.close(); } } catch (IOException e) { System.out.println("Error closing zip stream"); e.printStackTrace(); System.out.println("deleting zip file: " + imagesZipFile.delete()); throw aimsException; } } return zipCreated; }
From source file:org.deeplearning4j.models.embeddings.loader.WordVectorSerializer.java
private static void writeEntry(InputStream inputStream, ZipOutputStream zipStream) throws IOException { byte[] bytes = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(bytes)) != -1) { zipStream.write(bytes, 0, bytesRead); }/*from w w w . java2 s . co m*/ }
From source file:middleware.NewServerSocket.java
private boolean zipAllFiles() { File zipFile = new File(zipFileName); int index = 0; while (zipFile.exists()) { if (!zipFile.delete()) { ++index;// ww w.j a va 2 s . c o m zipFileName = "LogFiles_" + index + ".zip"; zipFile = new File(zipFileName); } } try { FileOutputStream fos = new FileOutputStream(zipFileName); ZipOutputStream zos = new ZipOutputStream(fos); File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) { // only sends the dstat log file. if (files[i].getName().contains("log_exp_1")) { FileInputStream fis = new FileInputStream(files[i]); // begin writing a new ZIP entry, positions the stream to the start of // the entry data zos.putNextEntry(new ZipEntry(files[i].getName())); int length; while ((length = fis.read(fileBuffer)) > 0) { zos.write(fileBuffer, 0, length); } zos.closeEntry(); // close the InputStream fis.close(); } } // close the ZipOutputStream zos.close(); } catch (IOException ioe) { return false; } return true; }
From source file:edu.umd.cs.marmoset.modelClasses.TestOutcome.java
/** * Zips the contents of a given file containing code coverage results in XML format. * @param file The file containing the code coverage XML results. * @throws IOException If there are any error reading the file. *///from www .j a va 2 s . com public void setCodeCoveralXMLResults(File file) throws IOException { if (!file.exists() || !file.canRead()) { return; } ByteArrayOutputStream actualData = new ByteArrayOutputStream(); ZipOutputStream zipOut = new ZipOutputStream(actualData); ZipEntry entry = new ZipEntry("coverage"); zipOut.putNextEntry(entry); BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); try { int bytesRead = 0; byte[] bytes = new byte[2048]; while (true) { int numBytes = in.read(bytes); if (numBytes == -1) break; zipOut.write(bytes, 0, numBytes); bytesRead += numBytes; } zipOut.closeEntry(); zipOut.close(); byte[] outbytes = actualData.toByteArray(); // System.out.println("outbytes.length: "+outbytes.length+ // " and we read: " +bytesRead+ " total bytes"); setDetails(outbytes); } finally { in.close(); } }
From source file:org.etudes.tool.melete.ExportMeleteModules.java
/** * @param inputFile -//from w ww . j a v a 2 s . c o m * file to zip * @param zout - * zip output stream * @throws FileNotFoundException * @throws IOException */ private void writeToZip(File inputFile, ZipOutputStream zout, String baseFileName) throws FileNotFoundException, IOException { File files[] = inputFile.listFiles(); for (int i = 0; i < files.length; i++) { if (files[i].isFile()) { FileInputStream in = new FileInputStream(files[i]); String name = null; String filepath = files[i].getAbsolutePath(); // name = filepath.substring(filepath.indexOf(baseFileName) // + baseFileName.length() + 1); // fix by Raul Enrique Mengod Lpez name = filepath.substring(filepath.indexOf(File.separator + baseFileName + File.separator) + baseFileName.length() + 2); zout.putNextEntry(new ZipEntry(name)); // Transfer bytes from the file to the ZIP file int len; byte buf[] = new byte[102400]; while ((len = in.read(buf)) > 0) { zout.write(buf, 0, len); } zout.closeEntry(); in.close(); } else { writeToZip(files[i], zout, baseFileName); } } }
From source file:com.hichinaschool.flashcards.async.DeckTask.java
private TaskData doInBackgroundExportApkg(TaskData... params) { // Log.i(AnkiDroidApp.TAG, "doInBackgroundExportApkg"); Object[] data = params[0].getObjArray(); String colPath = (String) data[0]; String apkgPath = (String) data[1]; boolean includeMedia = (Boolean) data[2]; byte[] buf = new byte[1024]; try {//from ww w . j av a 2 s.c om try { AnkiDb d = AnkiDatabaseManager.getDatabase(colPath); } catch (SQLiteDatabaseCorruptException e) { // collection is invalid return new TaskData(false); } finally { AnkiDatabaseManager.closeDatabase(colPath); } // export collection ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(apkgPath)); FileInputStream colFin = new FileInputStream(colPath); ZipEntry ze = new ZipEntry("collection.anki2"); zos.putNextEntry(ze); int len; while ((len = colFin.read(buf)) >= 0) { zos.write(buf, 0, len); } zos.closeEntry(); colFin.close(); // export media JSONObject media = new JSONObject(); if (includeMedia) { File mediaDir = new File(AnkiDroidApp.getCurrentAnkiDroidMediaDir()); if (mediaDir.exists() && mediaDir.isDirectory()) { File[] mediaFiles = mediaDir.listFiles(); int c = 0; for (File f : mediaFiles) { FileInputStream mediaFin = new FileInputStream(f); ze = new ZipEntry(Integer.toString(c)); zos.putNextEntry(ze); while ((len = mediaFin.read(buf)) >= 0) { zos.write(buf, 0, len); } zos.closeEntry(); media.put(Integer.toString(c), f.getName()); } } } ze = new ZipEntry("media"); zos.putNextEntry(ze); InputStream mediaIn = new ByteArrayInputStream(Utils.jsonToString(media).getBytes("UTF-8")); while ((len = mediaIn.read(buf)) >= 0) { zos.write(buf, 0, len); } zos.closeEntry(); zos.close(); } catch (FileNotFoundException e) { return new TaskData(false); } catch (IOException e) { return new TaskData(false); } catch (JSONException e) { return new TaskData(false); } return new TaskData(true); }
From source file:com.nit.async.DeckTask.java
private TaskData doInBackgroundExportApkg(TaskData... params) { // Log.i(AnkiDroidApp.TAG, "doInBackgroundExportApkg"); Object[] data = params[0].getObjArray(); String colPath = (String) data[0]; String apkgPath = (String) data[1]; boolean includeMedia = true; //(Boolean) data[2]; byte[] buf = new byte[1024]; try {//from w ww.j av a2s.c o m try { AnkiDb d = AnkiDatabaseManager.getDatabase(colPath); } catch (SQLiteDatabaseCorruptException e) { // collection is invalid return new TaskData(false); } finally { AnkiDatabaseManager.closeDatabase(colPath); } // export collection ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(apkgPath)); FileInputStream colFin = new FileInputStream(colPath); ZipEntry ze = new ZipEntry("collection.anki2"); zos.putNextEntry(ze); int len; while ((len = colFin.read(buf)) >= 0) { zos.write(buf, 0, len); } zos.closeEntry(); colFin.close(); // export media JSONObject media = new JSONObject(); if (includeMedia) { File mediaDir = new File(AnkiDroidApp.getCurrentAnkiDroidMediaDir()); if (mediaDir.exists() && mediaDir.isDirectory()) { File[] mediaFiles = mediaDir.listFiles(); int c = 0; for (File f : mediaFiles) { FileInputStream mediaFin = new FileInputStream(f); ze = new ZipEntry(Integer.toString(c)); zos.putNextEntry(ze); while ((len = mediaFin.read(buf)) >= 0) { zos.write(buf, 0, len); } zos.closeEntry(); media.put(Integer.toString(c), f.getName()); c++; } } } ze = new ZipEntry("media"); zos.putNextEntry(ze); InputStream mediaIn = new ByteArrayInputStream(Utils.jsonToString(media).getBytes("UTF-8")); while ((len = mediaIn.read(buf)) >= 0) { zos.write(buf, 0, len); } zos.closeEntry(); zos.close(); } catch (FileNotFoundException e) { return new TaskData(false); } catch (IOException e) { return new TaskData(false); } catch (JSONException e) { return new TaskData(false); } return new TaskData(true); }
From source file:br.org.indt.ndg.client.Service.java
/** * * @param surveyIds//from ww w . j av a 2 s . co m * @return * @throws NDGServerException */ public String downloadSurvey(String username, ArrayList<String> surveyIds) throws NDGServerException { final String SURVEY = "survey"; String strFileContent = null; byte[] fileContent = null; ArrayList<String> arrayStrFileContent; try { arrayStrFileContent = new ArrayList<String>(); for (int i = 0; i < surveyIds.size(); i++) { arrayStrFileContent.add(i, msmBD.loadSurveyFromServerToEditor(username, surveyIds.get(i))); } } catch (MSMApplicationException e) { e.printStackTrace(); throw new NDGServerException(e.getErrorCode()); } catch (Exception e) { e.printStackTrace(); throw new NDGServerException(UNEXPECTED_SERVER_EXCEPTION); } File f = new File(SURVEY + ZIP); ZipOutputStream out = null; try { out = new ZipOutputStream(new FileOutputStream(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } for (int i = 0; i < surveyIds.size(); i++) { StringBuilder sb = new StringBuilder(); sb.append(arrayStrFileContent.get(i)); File zipDir = new File(SURVEY + surveyIds.get(i)); ZipEntry e = new ZipEntry(zipDir + File.separator + "survey.xml"); try { out.putNextEntry(e); byte[] data = sb.toString().getBytes(); out.write(data, 0, data.length); out.closeEntry(); } catch (IOException e1) { e1.printStackTrace(); } } try { out.close(); fileContent = getBytesFromFile(f); strFileContent = Base64Encode.base64Encode(fileContent); } catch (IOException e1) { e1.printStackTrace(); } f.delete(); return strFileContent; }
From source file:org.openmrs.module.dhisconnector.api.impl.DHISConnectorServiceImpl.java
private void addDHIS2APIDirectories(File dirObj, ZipOutputStream out, String sourceDirectory) { File[] files = dirObj.listFiles(); byte[] tmpBuf = new byte[1024]; for (int i = 0; i < files.length; i++) { if (matchingDHIS2APIBackUpStructure(files[i])) { if (files[i].isDirectory()) { addDHIS2APIDirectories(files[i], out, sourceDirectory); continue; }/*from w w w .ja v a2s. c om*/ try { FileInputStream in = new FileInputStream(files[i].getAbsolutePath()); String entryPath = (new File(sourceDirectory)).toURI().relativize(files[i].toURI()).getPath(); System.out.println("Adding: " + entryPath); out.putNextEntry(new ZipEntry(entryPath)); int len; while ((len = in.read(tmpBuf)) > 0) { out.write(tmpBuf, 0, len); } out.closeEntry(); in.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:br.org.indt.ndg.client.Service.java
private void zipDir(String dir2zip, ZipOutputStream zos) { try {//from w w w.ja v a2 s. c o m File zipDir = new File(dir2zip); String[] dirList = zipDir.list(); byte[] readBuffer = new byte[2156]; int bytesIn = 0; 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); continue; } FileInputStream fis = new FileInputStream(f); ZipEntry anEntry = new ZipEntry(f.getPath()); zos.putNextEntry(anEntry); while ((bytesIn = fis.read(readBuffer)) != -1) { zos.write(readBuffer, 0, bytesIn); } fis.close(); } } catch (Exception e) { e.printStackTrace(); } }