List of usage examples for java.util.zip GZIPInputStream read
public int read(byte b[]) throws IOException
b.length
bytes of data from this input stream into an array of bytes. From source file:com.gelakinetic.mtgfam.helpers.CardDbAdapter.java
public static void copyDB(Context ctx) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ctx); SharedPreferences.Editor editor = preferences.edit(); try {//from w ww . j a v a 2 s . c o m File folder = new File(DB_PATH); if (!folder.exists()) { folder.mkdir(); } File db = new File(folder, DB_NAME); if (db.exists()) { db.delete(); editor.putString("lastUpdate", ""); editor.putInt("databaseVersion", -1); editor.commit(); } if (!db.exists()) { GZIPInputStream gis = new GZIPInputStream(ctx.getResources().openRawResource(R.raw.db)); FileOutputStream fos = new FileOutputStream(db); byte[] buffer = new byte[1024]; int length; while ((length = gis.read(buffer)) > 0) { fos.write(buffer, 0, length); } editor.putInt("databaseVersion", CardDbAdapter.DATABASE_VERSION); editor.commit(); // Close the streams fos.flush(); fos.close(); gis.close(); } } catch (NotFoundException e) { } catch (IOException e) { } catch (Exception e) { } }
From source file:XmldapCertsAndKeys.java
/** * Decodes data from Base64 notation, automatically detecting * gzip-compressed data and decompressing it. * /*from ww w . ja v a2 s . c o m*/ * @param s * the string to decode * @return the decoded data * @since 1.4 */ public static byte[] decode(String s) { byte[] bytes; try { bytes = s.getBytes(PREFERRED_ENCODING); } // end try catch (java.io.UnsupportedEncodingException uee) { bytes = s.getBytes(); } // end catch // </change> // Decode bytes = decode(bytes, 0, bytes.length); // Check to see if it's gzip-compressed // GZIP Magic Two-Byte Number: 0x8b1f (35615) if (bytes != null && bytes.length >= 4) { int head = (bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00); if (java.util.zip.GZIPInputStream.GZIP_MAGIC == head) { java.io.ByteArrayInputStream bais = null; java.util.zip.GZIPInputStream gzis = null; java.io.ByteArrayOutputStream baos = null; byte[] buffer = new byte[2048]; int length = 0; try { baos = new java.io.ByteArrayOutputStream(); bais = new java.io.ByteArrayInputStream(bytes); gzis = new java.util.zip.GZIPInputStream(bais); while ((length = gzis.read(buffer)) >= 0) { baos.write(buffer, 0, length); } // end while: reading input // No error? Get new bytes. bytes = baos.toByteArray(); } // end try catch (java.io.IOException e) { // Just return originally-decoded bytes } // end catch finally { try { baos.close(); } catch (Exception e) { } try { gzis.close(); } catch (Exception e) { } try { bais.close(); } catch (Exception e) { } } // end finally } // end if: gzipped } // end if: bytes.length >= 2 return bytes; }
From source file:es.mityc.firmaJava.libreria.utilidades.Base64.java
/** * Decodes data from Base64 notation, automatically * detecting gzip-compressed data and decompressing it. * * @param s the string to decode/*from ww w . j a v a2 s . c o m*/ * @param options encode options such as URL_SAFE * @return the decoded data * @since 1.4 */ public static byte[] decode(String s, int options) { byte[] bytes; try { bytes = s.getBytes(PREFERRED_ENCODING); } // end try catch (UnsupportedEncodingException uee) { bytes = s.getBytes(); } // end catch //</change> // Decode bytes = decode(bytes, 0, bytes.length, options); // Check to see if it's gzip-compressed // GZIP Magic Two-Byte Number: 0x8b1f (35615) int l = bytes.length; if (bytes != null && l >= 4) { int head = ((int) bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00); if (GZIPInputStream.GZIP_MAGIC == head) { ByteArrayInputStream bais = null; GZIPInputStream gzis = null; ByteArrayOutputStream baos = null; byte[] buffer = new byte[2048]; int length = 0; try { baos = new ByteArrayOutputStream(); bais = new ByteArrayInputStream(bytes); gzis = new GZIPInputStream(bais); int longitud = gzis.read(buffer); while ((length = longitud) >= 0) { baos.write(buffer, 0, length); longitud = gzis.read(buffer); } // end while: reading input // No error? Get new bytes. bytes = baos.toByteArray(); } // end try catch (IOException e) { // Just return originally-decoded bytes } // end catch finally { try { baos.close(); } catch (Exception e) { log.error(e); } try { gzis.close(); } catch (Exception e) { log.error(e); } try { bais.close(); } catch (Exception e) { log.error(e); } } // end finally } // end if: gzipped } // end if: bytes.length >= 2 return bytes; }
From source file:it.drwolf.ridire.session.async.Mapper.java
private StringWithEncoding getGuessedEncodingAndSetRawContentFromGZFile(File resourceFile) throws IOException { byte[] buf = new byte[Mapper.BUFLENGTH]; int count = 0; GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(resourceFile)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((count = gzis.read(buf)) != -1) { baos.write(buf, 0, count);/* w ww . j a va2s.c om*/ } gzis.close(); baos.close(); CharsetDetector charsetDetector = new CharsetDetector(); byte[] byteArray = baos.toByteArray(); charsetDetector.setText(byteArray); CharsetMatch[] matches = charsetDetector.detectAll(); String encoding = this.allowedCharsets.get(1); for (CharsetMatch cm : matches) { if (this.allowedCharsets.contains(cm.getName())) { encoding = cm.getName(); // System.out.println(encoding); break; } } String rawContent = new String(byteArray, encoding); return new StringWithEncoding(rawContent, encoding); }
From source file:it.drwolf.ridire.session.async.Mapper.java
@SuppressWarnings("unchecked") private StringWithEncoding transformPDF2HTML(File resourceFile, EntityManager entityManager) throws IOException, InterruptedException { String workingDirName = System.getProperty("java.io.tmpdir"); String userDir = System.getProperty("user.dir"); byte[] buf = new byte[Mapper.BUFLENGTH]; int count = 0; GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(resourceFile)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((count = gzis.read(buf)) != -1) { baos.write(buf, 0, count);//from www . jav a 2 s . c o m } gzis.close(); baos.close(); byte[] byteArray = baos.toByteArray(); String uuid = UUID.randomUUID().toString(); String pdfFileName = uuid + ".pdf"; String htmlFileName = uuid + ".html"; File tmpDir = new File(workingDirName); String htmlFileNameCompletePath = workingDirName + JobMapperMonitor.FILE_SEPARATOR + htmlFileName; File fileToConvert = new File(tmpDir, pdfFileName); FileUtils.writeByteArrayToFile(fileToConvert, byteArray); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); CommandParameter cp = entityManager.find(CommandParameter.class, CommandParameter.PDFTOHTML_EXECUTABLE_KEY); CommandLine commandLine = CommandLine.parse(cp.getCommandValue()); commandLine.addArgument("-c"); commandLine.addArgument("-i"); commandLine.addArgument(fileToConvert.getAbsolutePath()); commandLine.addArgument(htmlFileNameCompletePath); executor.setExitValue(0); executor.execute(commandLine); try { FileUtils.moveFileToDirectory( new File(userDir + JobMapperMonitor.FILE_SEPARATOR + uuid + "-outline.html"), tmpDir, false); } catch (IOException e) { } cp = entityManager.find(CommandParameter.class, CommandParameter.PDFCLEANER_EXECUTABLE_KEY); commandLine = CommandLine .parse("java -Xmx128m -jar -Djava.io.tmpdir=" + this.tempDir + " " + cp.getCommandValue()); commandLine.addArgument(htmlFileNameCompletePath); commandLine.addArgument("39"); commandLine.addArgument("6"); commandLine.addArgument("5"); executor = new DefaultExecutor(); executor.setExitValue(0); ExecuteWatchdog watchdog = new ExecuteWatchdog(Mapper.PDFCLEANER_TIMEOUT); executor.setWatchdog(watchdog); ByteArrayOutputStream baosStdOut = new ByteArrayOutputStream(1024); ExecuteStreamHandler executeStreamHandler = new PumpStreamHandler(baosStdOut, null, null); executor.setStreamHandler(executeStreamHandler); int exitValue = executor.execute(commandLine); String htmlString = null; if (exitValue == 0) { htmlString = baosStdOut.toString(); } FileUtils.deleteQuietly(new File(htmlFileNameCompletePath)); PrefixFileFilter pff = new PrefixFileFilter(uuid); for (File f : FileUtils.listFiles(tmpDir, pff, null)) { FileUtils.deleteQuietly(f); } if (htmlString != null) { htmlString = htmlString.replaceAll(" ", " "); htmlString = htmlString.replaceAll("<br.*?>", " "); CharsetDetector charsetDetector = new CharsetDetector(); charsetDetector.setText(htmlString.getBytes()); String encoding = charsetDetector.detect().getName(); return new StringWithEncoding(htmlString, encoding); } return null; }
From source file:de.zib.scalaris.TransactionSingleOpTest.java
/** * Tests how long it takes to read a large string with different compression * schemes./*from w w w . j ava 2 s.c om*/ * * @param compressed * how to compress * @param key * the key to append to the {@link #testTime} * * @throws ConnectionException * @throws UnknownException * @throws AbortException * @throws NotFoundException * @throws IOException */ protected void testReadLargeString(final int compression, final String key) throws ConnectionException, UnknownException, AbortException, NotFoundException, IOException { final StringBuilder sb = new StringBuilder(testData.length * 8 * 100); for (int i = 0; i < 100; ++i) { for (final String data : testData) { sb.append(data); } } final String expected = sb.toString(); final TransactionSingleOp conn = new TransactionSingleOp(); conn.setCompressed(true); switch (compression) { case 1: conn.setCompressed(false); case 2: conn.write(testTime + key, expected); break; case 3: conn.setCompressed(false); case 4: final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final GZIPOutputStream gos = new GZIPOutputStream(bos); gos.write(expected.getBytes("UTF-8")); gos.flush(); gos.close(); conn.write(testTime + key, new Base64(0).encodeToString(bos.toByteArray())); break; default: return; } try { for (int i = 0; i < 500; ++i) { String actual = conn.read(testTime + key).stringValue(); if (compression >= 3) { final byte[] packed = new Base64(0).decode(actual); final ByteArrayOutputStream unpacked = new ByteArrayOutputStream(); final ByteArrayInputStream bis = new ByteArrayInputStream(packed); final GZIPInputStream gis = new GZIPInputStream(bis); final byte[] bbuf = new byte[256]; int read = 0; while ((read = gis.read(bbuf)) >= 0) { unpacked.write(bbuf, 0, read); } gis.close(); actual = new String(unpacked.toString("UTF-8")); } assertEquals(expected, actual); } } finally { conn.closeConnection(); } }
From source file:configuration.Util.java
/** * This un-GZIP a the input_filename to the output_filename * @param input_filename/*from w ww . java2 s. co m*/ * @param output_filename * @return True if success */ public static boolean ungzip(String input_filename, String output_filename) { try { GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(input_filename)); OutputStream out = new FileOutputStream(output_filename); byte[] buffer = new byte[1024]; int len; while ((len = gzipInputStream.read(buffer)) > 0) { out.write(buffer, 0, len); } gzipInputStream.close(); out.close(); return true; } catch (Exception e) { System.out.println("ungzip Failed!"); System.out.println(e); e.printStackTrace(); return false; } }
From source file:iracing.webapi.IracingWebApi.java
private String getResponseText(URLConnection conn) throws IOException { byte[] bytes; // NOTE: The response is read into memory first before unzipping // because it resulted in a 200% performance increase // as opposed to unzipping directly from the connection inputstream // Read the response in to memory (GZipped or not) InputStream is = conn.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {//w ww . j a v a2s . com byte[] b = new byte[1024]; int bytesRead; while ((bytesRead = is.read(b)) != -1) { baos.write(b, 0, bytesRead); } bytes = baos.toByteArray(); } finally { baos.close(); is.close(); } // System.err.println(conn.getURL().toString()); this.bytesReceived += bytes.length; // Unzip the response if necessary if ("gzip".equals(conn.getContentEncoding())) { // System.err.println("compressed : " + bytesReceived.length + " bytes"); GZIPInputStream is2 = null; try { is2 = new GZIPInputStream(new ByteArrayInputStream(bytes)); baos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int bytesRead; while ((bytesRead = is2.read(b)) != -1) { baos.write(b, 0, bytesRead); } bytes = baos.toByteArray(); } finally { baos.close(); is2.close(); } } // System.err.println("uncompressed : " + bytesReceived.length + " bytes"); return new String(bytes); }
From source file:com.alcatel_lucent.nz.wnmsextract.reader.FileUtilities.java
public boolean decompressGZipFile(File input) { // Open the compressed file GZIPInputStream in = null; // If the file is of zero length delete it if (input.length() == 0) { deleteFile(input);/*from ww w . ja v a 2s. c o m*/ return true; } try { in = new GZIPInputStream(new FileInputStream(input)); } catch (FileNotFoundException e) { jlog.fatal(".gz file not found: " + input); e.printStackTrace(); return false; } catch (IOException e) { jlog.fatal("IO exception on: " + input); e.printStackTrace(); return false; } // Open the output file String target = input.getAbsolutePath().replace(".gz", ".xml"); OutputStream out = null; try { out = new FileOutputStream(target); } catch (FileNotFoundException e) { jlog.fatal("File not found: " + target); e.printStackTrace(); return false; } // Transfer bytes from the compressed file to the output file byte[] buf = new byte[1024]; int len; try { while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } } catch (IOException e) { jlog.fatal("IO exception when writing to " + target); e.printStackTrace(); return false; } finally { // Close the file and stream try { in.close(); } catch (IOException e) { jlog.fatal("Can't close input stream"); e.printStackTrace(); return false; } try { out.close(); } catch (IOException e) { jlog.fatal("Can't close output stream"); e.printStackTrace(); return false; } } // Check if the gzip file size == 0 File outputFile = new File(target); // If the file is of zero length something bad happened if (outputFile.length() == 0) { return false; } // Delete the file deleteFile(input); return true; }
From source file:edu.harvard.iq.dvn.core.web.study.AddFilesPage.java
private List<StudyFileEditBean> createStudyFilesFromTar(File uploadedInputFile) { List<StudyFileEditBean> fbList = new ArrayList<StudyFileEditBean>(); File dir = new File(uploadedInputFile.getParentFile(), study.getId().toString()); if (!dir.exists()) { dir.mkdir();/*from ww w. j a v a 2 s . com*/ } List<File> directoriesToDelete = new ArrayList<File>(); File unzippedFile = null; TarArchiveInputStream tiStream = null; FileOutputStream tempOutStream = null; String unzipError = ""; if (GzipUtils.isCompressedFilename(uploadedInputFile.getName())) { try { GZIPInputStream zippedInput = new GZIPInputStream(new FileInputStream(uploadedInputFile)); unzippedFile = new File(dir, "unzipped-file-" + UUID.randomUUID()); FileOutputStream unzippedOutput = new FileOutputStream(unzippedFile); byte[] dataBuffer = new byte[8192]; int i = 0; while ((i = zippedInput.read(dataBuffer)) > 0) { unzippedOutput.write(dataBuffer, 0, i); unzippedOutput.flush(); } tiStream = new TarArchiveInputStream(new FileInputStream(unzippedFile)); } catch (Exception ex) { unzipError = " A common gzip extension was found but is the file corrupt?"; } } else if (BZip2Utils.isCompressedFilename(uploadedInputFile.getName())) { try { BZip2CompressorInputStream zippedInput = new BZip2CompressorInputStream( new FileInputStream(uploadedInputFile)); unzippedFile = new File(dir, "unzipped-file-" + UUID.randomUUID()); FileOutputStream unzippedOutput = new FileOutputStream(unzippedFile); byte[] dataBuffer = new byte[8192]; int i = 0; while ((i = zippedInput.read(dataBuffer)) > 0) { unzippedOutput.write(dataBuffer, 0, i); unzippedOutput.flush(); } tiStream = new TarArchiveInputStream(new FileInputStream(unzippedFile)); } catch (Exception ex) { unzipError = " A common bzip2 extension was found but is the file corrupt?"; } } else { try { // no need to decompress, carry on tiStream = new TarArchiveInputStream(new FileInputStream(uploadedInputFile)); } catch (FileNotFoundException ex) { unzipError = " Is the tar file corrupt?"; } } TarArchiveEntry tEntry = null; if (tiStream == null) { String msg = "Problem reading uploaded file." + unzipError; setTarValidationErrorMessage(msg); uploadedInputFile.delete(); fbList = new ArrayList<StudyFileEditBean>(); return fbList; } try { while ((tEntry = tiStream.getNextTarEntry()) != null) { String fileEntryName = tEntry.getName(); if (!tEntry.isDirectory()) { if (fileEntryName != null && !fileEntryName.equals("")) { String dirName = null; String finalFileName = fileEntryName; int ind = fileEntryName.lastIndexOf('/'); if (ind > -1) { finalFileName = fileEntryName.substring(ind + 1); if (ind > 0) { dirName = fileEntryName.substring(0, ind); dirName = dirName.replace('/', '-'); } } else { finalFileName = fileEntryName; } // only process normal tar entries, not the ones that start with "._" because they were created on a mac: // http://superuser.com/questions/61185/why-do-i-get-files-like-foo-in-my-tarball-on-os-x // http://superuser.com/questions/212896/is-there-any-way-to-prevent-a-mac-from-creating-dot-underscore-files if (!finalFileName.startsWith("._")) { File tempUploadedFile = null; try { tempUploadedFile = FileUtil.createTempFile(dir, finalFileName); } catch (Exception ex) { Logger.getLogger(AddFilesPage.class.getName()).log(Level.SEVERE, null, ex); String msg = "Problem creating temporary file."; setTarValidationErrorMessage(msg); } tempOutStream = new FileOutputStream(tempUploadedFile); byte[] dataBuffer = new byte[8192]; int i = 0; while ((i = tiStream.read(dataBuffer)) > 0) { tempOutStream.write(dataBuffer, 0, i); tempOutStream.flush(); } tempOutStream.close(); try { StudyFileEditBean tempFileBean = new StudyFileEditBean(tempUploadedFile, studyService.generateFileSystemNameSequence(), study); tempFileBean.setSizeFormatted(tempUploadedFile.length()); if (dirName != null) { tempFileBean.getFileMetadata().setCategory(dirName); } fbList.add(tempFileBean); setTarValidationErrorMessage(null); } catch (Exception ex) { String msg = "Problem preparing files for ingest. Is the tar file corrupt?"; setTarValidationErrorMessage(msg); uploadedInputFile.delete(); tempUploadedFile.delete(); fbList = new ArrayList<StudyFileEditBean>(); } } } } else { File directory = new File(dir, fileEntryName); directory.mkdir(); directoriesToDelete.add(directory); } } } catch (IOException ex) { String msg = "Problem reading tar file. Is it corrupt?"; setTarValidationErrorMessage(msg); } // teardown and cleanup try { tiStream.close(); } catch (IOException ex) { Logger.getLogger(AddFilesPage.class.getName()).log(Level.SEVERE, null, ex); } if (tiStream != null) { try { tiStream.close(); } catch (IOException ex) { Logger.getLogger(AddFilesPage.class.getName()).log(Level.SEVERE, null, ex); } } if (tempOutStream != null) { try { tempOutStream.close(); } catch (IOException ex) { Logger.getLogger(AddFilesPage.class.getName()).log(Level.SEVERE, null, ex); } } uploadedInputFile.delete(); if (unzippedFile != null) { unzippedFile.delete(); } for (File dirToDelete : directoriesToDelete) { if (dirToDelete.exists()) { try { FileUtils.forceDelete(dirToDelete); } catch (IOException ex) { Logger.getLogger(AddFilesPage.class.getName()).log(Level.SEVERE, null, ex); } } } return fbList; }