List of usage examples for java.util.zip ZipInputStream read
public int read(byte[] b, int off, int len) throws IOException
From source file:org.kuali.ole.utility.CompressUtils.java
/** * Method to extract a given zipped bag file to a given output directory or to a temp directory if toDir is null. * * @param bagFilePath//www .ja va2 s . c om * @param toDir * @return * @throws IOException */ public File extractZippedBagFile(String bagFilePath, String toDir) throws IOException { File bagFile = new File(bagFilePath); File extractDir = null; if (toDir != null && toDir.trim().length() != 0) extractDir = new File(toDir); else extractDir = File.createTempFile("tmp", ".ext"); FileUtils.deleteQuietly(extractDir); extractDir.mkdirs(); byte[] buffer = new byte[BUFFER_SIZE]; ZipInputStream zip = new ZipInputStream(new BufferedInputStream(new FileInputStream(bagFile))); ZipEntry next; while ((next = zip.getNextEntry()) != null) { String name = next.getName().replace('\\', '/').replaceFirst("[^/]*/", ""); if (name.startsWith(DATA_DIR)) { File localFile = new File(extractDir, Normalizer.normalize(name.substring(DATA_DIR.length()), FILENAME_NORMALIZATION_FORM)); if (next.isDirectory()) { if (!localFile.exists() && !localFile.mkdir()) throw new IOException("error creating local directories in output directory"); } else { File parent = localFile.getParentFile(); if (!parent.exists() && !parent.mkdirs()) throw new IOException("error creating local directories in output directory"); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(localFile)); int bytesRead; while ((bytesRead = zip.read(buffer, 0, BUFFER_SIZE)) != -1) { bos.write(buffer, 0, bytesRead); } bos.close(); } } else { File localFile = new File(extractDir, name); if (next.isDirectory()) { if (!localFile.exists() && !localFile.mkdir()) throw new IOException("error creating local directories in output directory"); } else { File parent = localFile.getParentFile(); if (!parent.exists() && !parent.mkdirs()) throw new IOException("error creating local directories in output directory"); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(localFile)); int bytesRead; while ((bytesRead = zip.read(buffer, 0, BUFFER_SIZE)) != -1) { bos.write(buffer, 0, bytesRead); } bos.close(); } } } zip.close(); return extractDir; }
From source file:org.efaps.esjp.archives.Archive_Base.java
/** * Check if access will be granted to the cmd to create a root node. * * @param _parameter Parameter as passed by the eFaps API * @return new Return/*from www.j av a2 s. co m*/ * @throws EFapsException on error */ public Return createFromZip(final Parameter _parameter) throws EFapsException { final Context.FileParameter fileItem = Context.getThreadContext().getFileParameters().get("upload"); if (fileItem != null) { try { final InputStream input = fileItem.getInputStream(); final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(input)); ZipEntry entry; final FileUtil fileUtil = new FileUtil(); final List<File> files = new ArrayList<>(); while ((entry = zis.getNextEntry()) != null) { int size; final byte[] buffer = new byte[2048]; final File file = fileUtil.getFile(entry.getName()); if (!file.isHidden()) { files.add(file); final FileOutputStream fos = new FileOutputStream(file); final BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length); while ((size = zis.read(buffer, 0, buffer.length)) != -1) { bos.write(buffer, 0, size); } bos.flush(); bos.close(); } } for (final File file : files) { if (file.length() > 0) { final Context.FileParameter fileTmp = new FileItem(file); Context.getThreadContext().getFileParameters().put("upload", fileTmp); _parameter.getParameters().put("name", new String[] { fileTmp.getName() }); create(_parameter); } } } catch (final IOException e) { throw new EFapsException(Archive_Base.class, "createFromZip", e); } } return new Return(); }
From source file:org.kuali.ole.docstore.model.bagit.BagExtractor.java
/** * @param bagFile/*ww w. ja va 2 s . c o m*/ * @param outputDir * @throws IOException */ public static void extractBag(File bagFile, File outputDir) throws IOException { byte[] buffer = new byte[BUFFER_SIZE]; ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(bagFile))); ZipEntry next; LOG.info("extractBag bagFile.getAbsolutePath " + bagFile.getAbsolutePath()); LOG.info("extractBag outputDir.getAbsolutePath " + outputDir.getAbsolutePath()); while ((next = zis.getNextEntry()) != null) { LOG.info("next.getName " + next.getName()); System.out.println("next.getName " + next.getName()); //String name = next.getName().replaceFirst("[^/]*/", ""); String name = next.getName().replace('\\', '/').replaceFirst("[^/]*/", ""); //System.out.println("replace nameunix "+nameunixreplace.replace('\\','/').replaceFirst("[^/]*/", "")); // String name = next.getName(); LOG.info("name " + name); LOG.info("normalize " + Normalizer.normalize(name.substring(DATA_DIR.length()), FILENAME_NORMALIZATION_FORM)); LOG.info("substring " + name.substring(DATA_DIR.length())); LOG.info("DATA_DIR " + DATA_DIR); if (name.startsWith(DATA_DIR)) { LOG.info("in if name.startsWith(DATA_DIR)"); File localFile = new File(outputDir, Normalizer.normalize(name.substring(DATA_DIR.length()), FILENAME_NORMALIZATION_FORM)); if (next.isDirectory()) { LOG.info("in if next.isDirectory"); if (!localFile.exists() && !localFile.mkdir()) { throw new IOException("error creating local directories in output directory"); } } else { LOG.info("in else next.isDirectory"); File parent = localFile.getParentFile(); if (!parent.exists() && !parent.mkdirs()) { throw new IOException("error creating local directories in output directory"); } BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(localFile)); int bytesRead; while ((bytesRead = zis.read(buffer, 0, BUFFER_SIZE)) != -1) { bos.write(buffer, 0, bytesRead); } bos.close(); } } } zis.close(); }
From source file:org.wso2.carbon.integration.framework.utils.ArchiveManipulatorUtil.java
public void extractFile(String sourceFilePath, String extractedDir) throws IOException { FileOutputStream fileoutputstream = null; String fileDestination = extractedDir + File.separator; byte[] buf = new byte[1024]; ZipInputStream zipinputstream = null; ZipEntry zipentry;//from w w w . j av a 2 s . c om try { zipinputstream = new ZipInputStream(new FileInputStream(sourceFilePath)); zipentry = zipinputstream.getNextEntry(); while (zipentry != null) { //for each entry to be extracted String entryName = fileDestination + zipentry.getName(); entryName = entryName.replace('/', File.separatorChar); entryName = entryName.replace('\\', File.separatorChar); int n; File newFile = new File(entryName); if (zipentry.isDirectory()) { if (!newFile.exists()) { newFile.mkdirs(); } zipentry = zipinputstream.getNextEntry(); continue; } else { File resourceFile = new File(entryName.substring(0, entryName.lastIndexOf(File.separator))); if (!resourceFile.exists()) { if (!resourceFile.mkdirs()) { break; } } } fileoutputstream = new FileOutputStream(entryName); while ((n = zipinputstream.read(buf, 0, 1024)) > -1) { fileoutputstream.write(buf, 0, n); } fileoutputstream.close(); zipinputstream.closeEntry(); zipentry = zipinputstream.getNextEntry(); } zipinputstream.close(); } catch (IOException e) { log.error("Error on archive extraction ", e); throw new IOException("Error on archive extraction ", e); } finally { if (fileoutputstream != null) { fileoutputstream.close(); } if (zipinputstream != null) { zipinputstream.close(); } } }
From source file:net.zionsoft.obadiah.model.Bible.java
private void downloadTranslation(String url, String translationShortName, OnDownloadProgressListener onProgress) throws Exception { ZipInputStream zis = null; SQLiteDatabase db = null;//from w w w . ja v a 2 s.c om try { db = mDatabaseHelper.openDatabase(); if (db == null) { Analytics.trackException("Failed to open database."); throw new Exception("Failed to open database for writing"); } db.beginTransaction(); TranslationHelper.createTranslationTable(db, translationShortName); zis = new ZipInputStream(NetworkHelper.getStream(url)); final byte buffer[] = new byte[2048]; final ByteArrayOutputStream os = new ByteArrayOutputStream(); int downloaded = 0; int read; ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { os.reset(); while ((read = zis.read(buffer, 0, 2048)) != -1) os.write(buffer, 0, read); final byte[] bytes = os.toByteArray(); String fileName = entry.getName(); fileName = fileName.substring(0, fileName.length() - 5); // removes the trailing ".json" if (fileName.equals("books")) { TranslationHelper.saveBookNames(db, new JSONObject(new String(bytes, "UTF8"))); } else { final String[] parts = fileName.split("-"); final int bookIndex = Integer.parseInt(parts[0]); final int chapterIndex = Integer.parseInt(parts[1]); TranslationHelper.saveVerses(db, translationShortName, bookIndex, chapterIndex, new JSONObject(new String(bytes, "UTF8"))); } onProgress.onProgress(++downloaded / 12); } db.setTransactionSuccessful(); } finally { if (db != null) { if (db.inTransaction()) { db.endTransaction(); } mDatabaseHelper.closeDatabase(); } if (zis != null) { try { zis.close(); } catch (IOException e) { // we can't do much here } } } }
From source file:pt.ua.dicoogle.core.index.FileIndexer.java
/** * Index a given file// ww w . ja v a 2s . c om * @param writer Object that represents an index opened for writting * @param file File to index */ public void index(File file, boolean resume) throws FileHandlerException { if (file.canRead()) { if (file.isDirectory()) { String[] files = file.list(); if (files != null) { for (int i = 0; i < files.length; i++) { try { // Leave processor Thread.yield(); // IDLE - Free CPU for a while if (Settings.getInstance().getIndexerEffort() < 100) { Thread.sleep((100 - Settings.getInstance().getIndexerEffort()) * 20); } } catch (InterruptedException ex) { Logger.getLogger(FileIndexer.class.getName()).log(Level.SEVERE, null, ex); } index(new File(file, files[i]), resume); } } } else { boolean zip = false; /* Verify if zip is enable */ if (Settings.getInstance().isIndexZIPFiles()) { if (file.getAbsolutePath().endsWith(".zip")) { zip = true; FileInputStream fis = null; ZipInputStream zis = null; ZipEntry entry; try { fis = new FileInputStream(file.getAbsolutePath()); zis = new ZipInputStream(new BufferedInputStream(fis)); // // Read each entry from the ZipInputStream until no more entry found // indicated by a null return value of the getNextEntry() method. // while ((entry = zis.getNextEntry()) != null) { //System.out.println("Unzipping: " + entry.getName()); int size; byte[] buffer = new byte[2048]; FileOutputStream fos = new FileOutputStream(entry.getName()); BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length); while ((size = zis.read(buffer, 0, buffer.length)) != -1) { bos.write(buffer, 0, size); } bos.flush(); bos.close(); fos.close(); File tmpFile = new File(entry.getName()); putFileIndex(tmpFile, resume); tmpFile.delete(); } } catch (IOException e) { e.printStackTrace(); } finally { try { zis.close(); fis.close(); //System.out.println("Closing File..."); } catch (IOException ex) { Logger.getLogger(FileIndexer.class.getName()).log(Level.SEVERE, null, ex); } } } } if (!zip) putFileIndex(file, resume); } } }
From source file:sloca.json.bootstrap.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from w ww . jav a 2 s .c o m*/ * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/JSON"); PrintWriter out = response.getWriter(); //creates a new gson object //by instantiating a new factory object, set pretty printing, then calling the create method Gson gson = new GsonBuilder().setPrettyPrinting().create(); //creats a new json object for printing the desired json output ArrayList<String> errList = new ArrayList<String>(); JsonArray errMsg = new JsonArray(); DiskFileItemFactory factory = new DiskFileItemFactory(); // Set factory constraints // factory.setSizeThreshold(yourMaxMemorySize); // factory.setRepository(yourTempDirectory); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // upload.setSizeMax(yourMaxRequestSize); ServletContext servletContext = this.getServletConfig().getServletContext(); File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir"); // Parse the request List<FileItem> uploadItems = null; try { uploadItems = upload.parseRequest(request); } catch (FileUploadException ex) { errList.add("invalid file"); } String value = ""; for (FileItem uploadItem : uploadItems) { if (uploadItem.isFormField()) { String fieldName = uploadItem.getFieldName(); //System.out.println(fieldName); value = uploadItem.getString(); //System.out.println(value); } } JsonObject jsonOutput = new JsonObject(); if (value == null) { errList.add("missing token"); } else if (value.isEmpty()) { errList.add("blank token"); } else { try { JWTUtility.verify(value, SharedSecretManager.getSharedSecretKeyAdmin()); } catch (JWTException e) { errList.add("invalid token"); } } Iterator<FileItem> iter = uploadItems.iterator(); while (iter.hasNext()) { //System.out.println("entered iterator "); FileItem item = iter.next(); if (!item.isFormField()) { //System.out.println("entered is FormField"); InputStream is = item.getInputStream(); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is)); ZipEntry ze; while ((ze = zis.getNextEntry()) != null) { String filePath = repository + File.separator + ze.getName(); //System.out.println(filePath); if (ze.getName().equals("demographics.csv") | ze.getName().equals("location-lookup.csv") | ze.getName().equals("location.csv")) { // out.println("entered print if<br/>"); int count; byte data[] = new byte[BUFFER]; FileOutputStream fos = new FileOutputStream(filePath); BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER); while ((count = zis.read(data, 0, BUFFER)) != -1) { bos.write(data, 0, count); } bos.flush(); bos.close(); } } zis.close(); } } File[] files = new File(repository.toString()).listFiles(); boolean isLocLookUpInserted = false; boolean isDemoInserted = false; boolean isLocInserted = false; //if uploaded folder does not contain the files, it will go back to bootstrap.jsp if (!isValidFile(files)) { errList.add("invalid file"); } if (errList.isEmpty()) { while (!isLocLookUpInserted || !isDemoInserted || !isLocInserted) { // for (File file : files) { String fileName = file.getName(); String filePath = repository + File.separator + fileName; //out.println("</br>" + filePath); BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath))); br.readLine(); String line = null; if (!isDemoInserted && fileName.contains("demographics.csv")) { BootStrapManager.processDemo(filePath); isDemoInserted = true; } if (!isLocLookUpInserted && fileName.contains("location-lookup.csv")) { BootStrapManager.processLocLookUp(filePath); isLocLookUpInserted = true; } if (isDemoInserted && isLocLookUpInserted && !isLocInserted && fileName.contains("location.csv")) { BootStrapManager.processLoc(filePath); isLocInserted = true; } br.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (br != null) { br.close(); } } file.delete(); } } //error List ArrayList<BootstrapError> locErrorList = BootStrapManager.locErrorList; ArrayList<BootstrapError> locLookUpErrorList = BootStrapManager.locLookUpErrorList; ArrayList<BootstrapError> demoErrorList = BootStrapManager.demoErrorList; Collections.sort(locErrorList); Collections.sort(locLookUpErrorList); Collections.sort(demoErrorList); if (locErrorList.isEmpty() && locLookUpErrorList.isEmpty() && demoErrorList.isEmpty()) { jsonOutput.addProperty("status", "success"); //add the array to output jsonOutput.add("num-record-loaded", getNumLoaded()); } else { jsonOutput.addProperty("status", "error"); jsonOutput.add("num-record-loaded", getNumLoaded()); //create a json array of error JsonArray error = new JsonArray(); //check demogrpahics file if (!demoErrorList.isEmpty()) { for (BootstrapError bse : demoErrorList) { JsonObject demoError = new JsonObject(); demoError.addProperty("file", "demographics.csv"); demoError.addProperty("line", bse.getLineNum()); //get Errors ArrayList<String> bseErr = bse.getErrMsg(); JsonArray errMsg2 = new JsonArray(); for (String e : bseErr) { errMsg2.add(new JsonPrimitive(e)); } demoError.add("message", errMsg2); error.add(demoError); } } //check location lookup file if (!locLookUpErrorList.isEmpty()) { for (BootstrapError bse : locLookUpErrorList) { JsonObject demoError = new JsonObject(); demoError.addProperty("file", "location-lookup.csv"); demoError.addProperty("line", bse.getLineNum()); //get Errors ArrayList<String> bseErr = bse.getErrMsg(); JsonArray errMsg2 = new JsonArray(); for (String e : bseErr) { errMsg2.add(new JsonPrimitive(e)); } demoError.add("message", errMsg2); error.add(demoError); } } //check location file if (!locErrorList.isEmpty()) { for (BootstrapError bse : locErrorList) { JsonObject demoError = new JsonObject(); demoError.addProperty("file", "location.csv"); demoError.addProperty("line", bse.getLineNum()); //get Errors ArrayList<String> bseErr = bse.getErrMsg(); JsonArray errMsg2 = new JsonArray(); for (String e : bseErr) { errMsg2.add(new JsonPrimitive(e)); } demoError.add("message", errMsg2); error.add(demoError); } } jsonOutput.add("error", error); } //display output out.println(gson.toJson(jsonOutput)); } else { jsonOutput.addProperty("status", "error"); //sorting the errlist into alphabetical order HashSet hs = new HashSet(); hs.addAll(errList); errList.clear(); errList.addAll(hs); Collections.sort(errList); //loop through the errors to add into the json error array for (String err : errList) { errMsg.add(new JsonPrimitive(err)); } jsonOutput.add("messages", errMsg); out.println(gson.toJson(jsonOutput)); } out.close(); }
From source file:org.kootox.episodesmanager.services.databases.TheTvDbService.java
protected void extractZip(File source, File destinationDirectory) { ZipInputStream zipIS = null; FileOutputStream out = null;/*from ww w.jav a 2 s .c om*/ try { byte[] buf = new byte[1024]; ZipEntry zipEntry; zipIS = new ZipInputStream(new FileInputStream(source)); zipEntry = zipIS.getNextEntry(); while (zipEntry != null) { //for each entry to be extracted String entryName = zipEntry.getName(); if (log.isDebugEnabled()) { log.debug("Extracting : " + entryName); } int n; File newFile = new File(destinationDirectory, entryName); out = new FileOutputStream(newFile); while ((n = zipIS.read(buf, 0, 1024)) > -1) out.write(buf, 0, n); out.close(); zipIS.closeEntry(); zipEntry = zipIS.getNextEntry(); } //while } catch (Exception eee) { log.error("Could not extract zip file", eee); } finally { try { if (zipIS != null) { zipIS.close(); } } catch (IOException eee) { log.debug("Could not close zip file", eee); } try { if (null != out) { out.close(); } } catch (IOException eee) { log.debug("Could not close extracted file", eee); } } }
From source file:org.wso2.carbon.automation.extensions.servers.utils.ArchiveExtractor.java
public void extractFile(String sourceFilePath, String extractedDir) throws IOException { FileOutputStream fileoutputstream = null; String fileDestination = extractedDir + File.separator; byte[] buf = new byte[1024]; ZipInputStream zipinputstream = null; ZipEntry zipentry;//www .ja v a 2 s . co m try { zipinputstream = new ZipInputStream(new FileInputStream(sourceFilePath)); zipentry = zipinputstream.getNextEntry(); while (zipentry != null) { //for each entry to be extracted String entryName = fileDestination + zipentry.getName(); entryName = entryName.replace('/', File.separatorChar); entryName = entryName.replace('\\', File.separatorChar); int n; File newFile = new File(entryName); boolean fileCreated = false; if (zipentry.isDirectory()) { if (!newFile.exists()) { fileCreated = newFile.mkdirs(); } zipentry = zipinputstream.getNextEntry(); continue; } else { File resourceFile = new File(entryName.substring(0, entryName.lastIndexOf(File.separator))); if (!resourceFile.exists()) { if (!resourceFile.mkdirs()) { break; } } } fileoutputstream = new FileOutputStream(entryName); while ((n = zipinputstream.read(buf, 0, 1024)) > -1) { fileoutputstream.write(buf, 0, n); } fileoutputstream.close(); zipinputstream.closeEntry(); zipentry = zipinputstream.getNextEntry(); } zipinputstream.close(); } catch (IOException e) { log.error("Error on archive extraction ", e); throw new IOException("Error on archive extraction ", e); } finally { if (fileoutputstream != null) { fileoutputstream.close(); } if (zipinputstream != null) { zipinputstream.close(); } } }
From source file:com.fdt.sdl.admin.ui.action.UploadAction.java
public void extractZip(InputStream is) throws IOException { ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is)); int BUFFER = 4096; BufferedOutputStream dest = null; ZipEntry entry;//from w w w . ja v a 2s . c o m while ((entry = zis.getNextEntry()) != null) { // System.out.println("Extracting: " +entry); // out.println(entry.getName()); String entryPath = entry.getName(); if (entryPath.indexOf('\\') >= 0) { entryPath = entryPath.replace('\\', File.separatorChar); } String destFilePath = WebserverStatic.getRootDirectory() + entryPath; int count; byte data[] = new byte[BUFFER]; // write the files to the disk File f = new File(destFilePath); if (!f.getParentFile().exists()) { f.getParentFile().mkdirs(); } if (!f.exists()) { FileUtil.createNewFile(f); logger.info("creating file: " + f); } else { logger.info("already existing file: " + f); if (f.isDirectory()) { continue; } } FileOutputStream fos = new FileOutputStream(f); dest = new BufferedOutputStream(fos, BUFFER); while ((count = zis.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, count); } dest.flush(); dest.close(); } zis.close(); }