List of usage examples for java.util.zip ZipEntry getSize
public long getSize()
From source file:org.jdesktop.wonderland.artupload.ArtUploadServlet.java
/** * Write files to the art directory/*from w w w .j a v a 2s . c om*/ * @param items the list of items containing the files to write * @throws ServletException if there is an error writing the files */ protected void writeFiles(List<FileItem> items) throws IOException, ServletException { // get the value of the "name" field FileItem nameItem = findItem(items, "name"); String name = nameItem.getString(); // write the model file FileItem modelItem = findItem(items, "model"); File modelsDir = new File(Util.getArtDir(getServletContext()), "models"); File modelFile = new File(modelsDir, name + ".j3s.gz"); try { modelItem.write(modelFile); } catch (Exception ex) { throw new ServletException(ex); } // unzip the textures FileItem texturesItem = findItem(items, "textures"); ZipInputStream zin = new ZipInputStream(texturesItem.getInputStream()); ZipEntry entry; File curDir = new File(Util.getArtDir(getServletContext()), "textures"); while ((entry = zin.getNextEntry()) != null) { if (entry.isDirectory()) { File dir = new File(curDir, entry.getName()); dir.mkdirs(); } else { // write the unzipped texture data File texture = new File(curDir, entry.getName()); FileOutputStream out = new FileOutputStream(texture); byte[] buffer; if (entry.getSize() > -1) { buffer = new byte[(int) entry.getSize()]; } else { buffer = new byte[64 * 1024]; } int read = 0; while ((read = zin.read(buffer, 0, buffer.length)) > -1) { out.write(buffer, 0, read); } out.close(); } } }
From source file:org.ambraproject.article.service.IngesterImpl.java
/** * Process files from the archive and store them to the {@link FileStoreService} * * @param archive - the archive being ingested * @param doi/*from w w w. j ava 2 s . c om*/ * @throws java.io.IOException - if there's a problem reading from the zip file * @throws org.ambraproject.filestore.FileStoreException * - if there's a problem writing files to the file store */ private void storeFiles(final ZipFile archive, String doi) throws IOException, FileStoreException { log.info("Removing existing files (if any) for {}", doi); try { documentManagementService.removeFromFileSystem(doi); } catch (Exception e) { throw new FileStoreException("Error removing existing files from the file store", e); } log.info("Storing files from archive {} to the file store", archive.getName()); Enumeration<? extends ZipEntry> entries = archive.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (!entry.getName().equalsIgnoreCase("manifest.dtd")) { InputStream inputStream = null; OutputStream outputStream = null; try { inputStream = archive.getInputStream(entry); outputStream = fileStoreService.getFileOutStream(FSIDMapper.zipToFSID(doi, entry.getName()), entry.getSize()); fileStoreService.copy(inputStream, outputStream); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { log.warn("Error closing input stream while writing files", e); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { log.warn("Error closing output stream while writing files", e); } } } } } log.info("Finished storing files from archive {}", archive.getName()); }
From source file:com.hichinaschool.flashcards.libanki.Media.java
/** * Extract zip data.//w ww.j a va2s .c o m * * @param zipData An input stream that represents a zipped file. * @return True if finished. */ public boolean syncAdd(File zipData) { boolean finished = false; ZipFile z = null; ArrayList<Object[]> media = new ArrayList<Object[]>(); long sizecnt = 0; JSONObject meta = null; int nextUsn = 0; try { z = new ZipFile(zipData, ZipFile.OPEN_READ); // get meta info first ZipEntry metaEntry = z.getEntry("_meta"); // if (metaEntry.getSize() >= 100000) { // Log.e(AnkiDroidApp.TAG, "Size for _meta entry found too big (" + z.getEntry("_meta").getSize() + ")"); // return false; // } meta = new JSONObject(Utils.convertStreamToString(z.getInputStream(metaEntry))); ZipEntry usnEntry = z.getEntry("_usn"); String usnstr = Utils.convertStreamToString(z.getInputStream(usnEntry)); nextUsn = Integer.parseInt(usnstr); } catch (JSONException e) { throw new RuntimeException(e); } catch (ZipException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } // Then loop through all files for (ZipEntry zentry : Collections.list(z.entries())) { // Check for zip bombs sizecnt += zentry.getSize(); if (sizecnt > 100 * 1024 * 1024) { Log.e(AnkiDroidApp.TAG, "Media zip file exceeds 100MB uncompressed, aborting unzipping"); return false; } if (zentry.getName().compareTo("_meta") == 0 || zentry.getName().compareTo("_usn") == 0) { // Ignore previously retrieved meta continue; } else if (zentry.getName().compareTo("_finished") == 0) { finished = true; } else { String name = meta.optString(zentry.getName()); if (illegal(name)) { continue; } String path = getDir().concat(File.separator).concat(name); try { Utils.writeToFile(z.getInputStream(zentry), path); } catch (IOException e) { throw new RuntimeException(e); } String csum = Utils.fileChecksum(path); // append db media.add(new Object[] { name, csum, _mtime(name) }); mMediaDb.execute("delete from log where fname = ?", new String[] { name }); } } // update media db and note new starting usn if (!media.isEmpty()) { mMediaDb.executeMany("insert or replace into media values (?,?,?)", media); } setUsn(nextUsn); // commits // if we have finished adding, we need to record the new folder mtime // so that we don't trigger a needless scan if (finished) { syncMod(); } return finished; }
From source file:org.opencms.importexport.CmsImportHelper.java
/** * Returns a byte array containing the content of the file.<p> * * @param filename the name of the file to read, relative to the folder or zip file * /* w w w .j a va 2 s . c om*/ * @return a byte array containing the content of the file * * @throws CmsImportExportException if something goes wrong */ public byte[] getFileBytes(String filename) throws CmsImportExportException { try { // is this a zip-file? if (getZipFile() != null) { // yes ZipEntry entry = getZipFile().getEntry(filename); // path to file might be relative, too if ((entry == null) && filename.startsWith("/")) { entry = m_zipFile.getEntry(filename.substring(1)); } if (entry == null) { throw new ZipException(Messages.get().getBundle() .key(Messages.LOG_IMPORTEXPORT_FILE_NOT_FOUND_IN_ZIP_1, filename)); } InputStream stream = getZipFile().getInputStream(entry); int size = new Long(entry.getSize()).intValue(); return CmsFileUtil.readFully(stream, size); } else { // no - use directory File file = new File(getFolder(), filename); return CmsFileUtil.readFile(file); } } catch (FileNotFoundException fnfe) { CmsMessageContainer msg = Messages.get().container(Messages.ERR_IMPORTEXPORT_FILE_NOT_FOUND_1, filename); if (LOG.isErrorEnabled()) { LOG.error(msg.key(), fnfe); } throw new CmsImportExportException(msg, fnfe); } catch (IOException ioe) { CmsMessageContainer msg = Messages.get().container(Messages.ERR_IMPORTEXPORT_ERROR_READING_FILE_1, filename); if (LOG.isErrorEnabled()) { LOG.error(msg.key(), ioe); } throw new CmsImportExportException(msg, ioe); } }
From source file:nodomain.freeyourgadget.gadgetbridge.devices.pebble.PBWReader.java
public PBWReader(Uri uri, Context context, String platform) throws IOException { uriHelper = UriHelper.get(uri, context); if (uriHelper.getFileName().endsWith(".pbl")) { STM32CRC stm32crc = new STM32CRC(); try (InputStream fin = uriHelper.openInputStream()) { byte[] buf = new byte[2000]; while (fin.available() > 0) { int count = fin.read(buf); stm32crc.addData(buf, count); }/*from w w w . j a va 2 s . co m*/ } int crc = stm32crc.getResult(); // language file app = new GBDeviceApp(UUID.randomUUID(), "Language File", "unknown", "unknown", GBDeviceApp.Type.UNKNOWN); pebbleInstallables = new ArrayList<>(); pebbleInstallables.add(new PebbleInstallable("lang", (int) uriHelper.getFileSize(), crc, PebbleProtocol.PUTBYTES_TYPE_FILE)); isValid = true; isLanguage = true; return; } String platformDir = ""; if (!uriHelper.getFileName().endsWith(".pbz")) { platformDir = determinePlatformDir(uriHelper, platform); if (platform.equals("chalk") && platformDir.equals("")) { return; } } LOG.info("using platformdir: '" + platformDir + "'"); String appName = null; String appCreator = null; String appVersion = null; UUID appUUID = null; ZipEntry ze; pebbleInstallables = new ArrayList<>(); byte[] buffer = new byte[1024]; int count; try (ZipInputStream zis = new ZipInputStream(uriHelper.openInputStream())) { while ((ze = zis.getNextEntry()) != null) { String fileName = ze.getName(); if (fileName.equals(platformDir + "manifest.json")) { long bytes = ze.getSize(); if (bytes > 8192) // that should be too much break; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((count = zis.read(buffer)) != -1) { baos.write(buffer, 0, count); } String jsonString = baos.toString(); try { JSONObject json = new JSONObject(jsonString); HashMap<String, Byte> fileTypeMap; try { JSONObject firmware = json.getJSONObject("firmware"); fileTypeMap = fwFileTypesMap; isFirmware = true; hwRevision = firmware.getString("hwrev"); } catch (JSONException e) { fileTypeMap = appFileTypesMap; isFirmware = false; } for (Map.Entry<String, Byte> entry : fileTypeMap.entrySet()) { try { JSONObject jo = json.getJSONObject(entry.getKey()); String name = jo.getString("name"); int size = jo.getInt("size"); long crc = jo.getLong("crc"); byte type = entry.getValue(); pebbleInstallables .add(new PebbleInstallable(platformDir + name, size, (int) crc, type)); LOG.info("found file to install: " + platformDir + name); isValid = true; } catch (JSONException e) { // not fatal } } } catch (JSONException e) { // no JSON at all that is a problem isValid = false; e.printStackTrace(); break; } } else if (fileName.equals("appinfo.json")) { long bytes = ze.getSize(); if (bytes > 500000) { LOG.warn(fileName + " exeeds maximum of 500000 bytes"); // that should be too much break; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((count = zis.read(buffer)) != -1) { baos.write(buffer, 0, count); } String jsonString = baos.toString(); try { JSONObject json = new JSONObject(jsonString); appName = json.getString("shortName"); appCreator = json.getString("companyName"); appVersion = json.getString("versionLabel"); appUUID = UUID.fromString(json.getString("uuid")); if (json.has("appKeys")) { mAppKeys = json.getJSONObject("appKeys"); LOG.info("found appKeys:" + mAppKeys.toString()); } } catch (JSONException e) { isValid = false; e.printStackTrace(); break; } } else if (fileName.equals(platformDir + "pebble-app.bin")) { zis.read(buffer, 0, 108); byte[] tmp_buf = new byte[32]; ByteBuffer buf = ByteBuffer.wrap(buffer); buf.order(ByteOrder.LITTLE_ENDIAN); buf.getLong(); // header, TODO: verify buf.getShort(); // struct version, TODO: verify mSdkVersion = buf.getShort(); mAppVersion = buf.getShort(); buf.getShort(); // size buf.getInt(); // offset buf.getInt(); // crc buf.get(tmp_buf, 0, 32); // app name buf.get(tmp_buf, 0, 32); // author mIconId = buf.getInt(); LOG.info("got icon id from pebble-app.bin: " + mIconId); buf.getInt(); // symbol table addr mFlags = buf.getInt(); LOG.info("got flags from pebble-app.bin: " + mFlags); // more follows but, not interesting for us } } if (appUUID != null && appName != null && appCreator != null && appVersion != null) { GBDeviceApp.Type appType = GBDeviceApp.Type.APP_GENERIC; if ((mFlags & 16) == 16) { appType = GBDeviceApp.Type.APP_ACTIVITYTRACKER; } else if ((mFlags & 1) == 1) { appType = GBDeviceApp.Type.WATCHFACE; } app = new GBDeviceApp(appUUID, appName, appCreator, appVersion, appType); } else if (!isFirmware) { isValid = false; } } }
From source file:org.apache.taverna.robundle.TestBundles.java
@Ignore("Broken in OpenJDK8 zipfs") @Test//from ww w .j a va 2 s. c o m public void mimeTypePosition() throws Exception { Bundle bundle = Bundles.createBundle(); String mimetype = "application/x-test"; Bundles.setMimeType(bundle, mimetype); assertEquals(mimetype, Bundles.getMimeType(bundle)); Path zip = Bundles.closeBundle(bundle); assertTrue(Files.exists(zip)); try (ZipFile zipFile = new ZipFile(zip.toFile())) { // Must be first entry ZipEntry mimeEntry = zipFile.entries().nextElement(); assertEquals("First zip entry is not 'mimetype'", "mimetype", mimeEntry.getName()); assertEquals("mimetype should be uncompressed, but compressed size mismatch", mimeEntry.getCompressedSize(), mimeEntry.getSize()); assertEquals("mimetype should have STORED method", ZipEntry.STORED, mimeEntry.getMethod()); assertEquals("Wrong mimetype", mimetype, IOUtils.toString(zipFile.getInputStream(mimeEntry), "ASCII")); } // Check position 30++ according to // http://livedocs.adobe.com/navigator/9/Navigator_SDK9_HTMLHelp/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Navigator_SDK9_HTMLHelp&file=Appx_Packaging.6.1.html#1522568 byte[] expected = ("mimetype" + mimetype + "PK").getBytes("ASCII"); FileInputStream in = new FileInputStream(zip.toFile()); byte[] actual = new byte[expected.length]; try { assertEquals(MIME_OFFSET, in.skip(MIME_OFFSET)); assertEquals(expected.length, in.read(actual)); } finally { in.close(); } assertArrayEquals(expected, actual); }
From source file:org.jdesktop.wonderland.artupload.UploadServlet.java
/** * Write files to the specified directory * @param items the list of items containing the files to write * @throws IOException if there is an error writing the files * @throws ServletException if there is an error writing the files *//* ww w . j a va2 s .co m*/ private void writeFiles(List<FileItem> items) throws IOException, ServletException { // get the value of the "name" field FileItem nameItem = findItem(items, "name"); String name = nameItem.getString(); // write the model file FileItem modelItem = findItem(items, "model"); File modelsDir = new File(Util.getArtDir(getServletContext()), "models"); File modelFile = new File(modelsDir, name + ".j3s.gz"); try { modelItem.write(modelFile); } catch (Exception ex) { throw new ServletException(ex); } // unzip the textures FileItem texturesItem = findItem(items, "textures"); ZipInputStream zin = new ZipInputStream(texturesItem.getInputStream()); ZipEntry entry; File curDir = new File(Util.getArtDir(getServletContext()), "textures"); while ((entry = zin.getNextEntry()) != null) { if (entry.isDirectory()) { File dir = new File(curDir, entry.getName()); dir.mkdirs(); } else { // write the unzipped texture data File texture = new File(curDir, entry.getName()); FileOutputStream out = new FileOutputStream(texture); byte[] buffer; if (entry.getSize() > -1) { buffer = new byte[(int) entry.getSize()]; } else { buffer = new byte[64 * 1024]; } int read = 0; while ((read = zin.read(buffer, 0, buffer.length)) > -1) { out.write(buffer, 0, read); } out.close(); } } }
From source file:org.apache.taverna.commandline.TavernaCommandLineTest.java
private void assertZipFilesEqual(File file1, File file2) { ZipFile zipFile1 = null;/*www . j av a 2 s. co m*/ ZipFile zipFile2 = null; try { zipFile1 = new ZipFile(file1); zipFile2 = new ZipFile(file2); } catch (Exception e) { assertTrue(String.format("%s and %s are not both zip files"), zipFile1 == null); } if (zipFile1 != null && zipFile2 != null) { Enumeration<? extends ZipEntry> entries1 = zipFile1.entries(); Enumeration<? extends ZipEntry> entries2 = zipFile2.entries(); while (entries1.hasMoreElements()) { assertTrue(entries2.hasMoreElements()); ZipEntry zipEntry1 = entries1.nextElement(); ZipEntry zipEntry2 = entries2.nextElement(); assertEquals(String.format("%s and %s are not both directories", zipEntry1, zipEntry2), zipEntry1.isDirectory(), zipEntry2.isDirectory()); assertEquals(String.format("%s and %s have different names", zipEntry1, zipEntry2), zipEntry1.getName(), zipEntry2.getName()); assertEquals(String.format("%s and %s have different sizes", zipEntry1, zipEntry2), zipEntry1.getSize(), zipEntry2.getSize()); try { byte[] byteArray1 = IOUtils.toByteArray(zipFile1.getInputStream(zipEntry1)); byte[] byteArray2 = IOUtils.toByteArray(zipFile2.getInputStream(zipEntry2)); assertArrayEquals(String.format("%s != %s", zipEntry1, zipEntry2), byteArray1, byteArray2); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } assertFalse(entries2.hasMoreElements()); } }
From source file:org.intermine.web.search.ClassAttributes.java
private static void loadIndexFromDatabase(ObjectStore os, String path) { long time = System.currentTimeMillis(); LOG.info("Attempting to restore search index from database..."); if (os instanceof ObjectStoreInterMineImpl) { Database db = ((ObjectStoreInterMineImpl) os).getDatabase(); try {//from w w w . j av a 2 s .c om InputStream is = MetadataManager.readLargeBinary(db, MetadataManager.SEARCH_INDEX); if (is != null) { GZIPInputStream gzipInput = new GZIPInputStream(is); ObjectInputStream objectInput = new ObjectInputStream(gzipInput); try { Object object = objectInput.readObject(); if (object instanceof LuceneIndexContainer) { index = (LuceneIndexContainer) object; LOG.info("Successfully restored search index information" + " from database in " + (System.currentTimeMillis() - time) + " ms"); LOG.info("Index: " + index); } else { LOG.warn("Object from DB has wrong class:" + object.getClass().getName()); } } finally { objectInput.close(); gzipInput.close(); } } else { LOG.warn("IS is null"); } if (index != null) { time = System.currentTimeMillis(); LOG.info("Attempting to restore search directory from database..."); is = MetadataManager.readLargeBinary(db, MetadataManager.SEARCH_INDEX_DIRECTORY); if (is != null) { if ("FSDirectory".equals(index.getDirectoryType())) { final int bufferSize = 2048; File directoryPath = new File(path + File.separator + LUCENE_INDEX_DIR); LOG.info("Directory path: " + directoryPath); // make sure we start with a new index if (directoryPath.exists()) { String[] files = directoryPath.list(); for (int i = 0; i < files.length; i++) { LOG.info("Deleting old file: " + files[i]); new File(directoryPath.getAbsolutePath() + File.separator + files[i]).delete(); } } else { directoryPath.mkdir(); } ZipInputStream zis = new ZipInputStream(is); ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { LOG.info("Extracting: " + entry.getName() + " (" + entry.getSize() + " MB)"); FileOutputStream fos = new FileOutputStream( directoryPath.getAbsolutePath() + File.separator + entry.getName()); BufferedOutputStream bos = new BufferedOutputStream(fos, bufferSize); int count; byte[] data = new byte[bufferSize]; while ((count = zis.read(data, 0, bufferSize)) != -1) { bos.write(data, 0, count); } bos.flush(); bos.close(); } FSDirectory directory = FSDirectory.open(directoryPath); index.setDirectory(directory); LOG.info("Successfully restored FS directory from database in " + (System.currentTimeMillis() - time) + " ms"); time = System.currentTimeMillis(); } else if ("RAMDirectory".equals(index.getDirectoryType())) { GZIPInputStream gzipInput = new GZIPInputStream(is); ObjectInputStream objectInput = new ObjectInputStream(gzipInput); try { Object object = objectInput.readObject(); if (object instanceof FSDirectory) { RAMDirectory directory = (RAMDirectory) object; index.setDirectory(directory); time = System.currentTimeMillis() - time; LOG.info("Successfully restored RAM directory" + " from database in " + time + " ms"); } } finally { objectInput.close(); gzipInput.close(); } } else { LOG.warn("Unknown directory type specified: " + index.getDirectoryType()); } LOG.info("Directory: " + index.getDirectory()); } else { LOG.warn("index is null!"); } } } catch (ClassNotFoundException e) { LOG.error("Could not load search index", e); } catch (SQLException e) { LOG.error("Could not load search index", e); } catch (IOException e) { LOG.error("Could not load search index", e); } } else { LOG.error("ObjectStore is of wrong type!"); } }
From source file:plugspud.PluginManager.java
/** * Unzip a file/*from ww w. j av a2s . c o m*/ * * @param zipFile * zip file * @param dir * directory to unzip to * @throws IOException * on any i/o errors */ public void unzip(File zipFile, File dir) throws IOException { context.log(PluginHostContext.LOG_INFORMATION, "Unzipping " + zipFile.getAbsolutePath() + " to " + dir.getAbsolutePath()); InputStream in = null; try { in = new BufferedInputStream(new FileInputStream(zipFile)); ZipInputStream zin = new ZipInputStream(in); ZipEntry e; while ((e = zin.getNextEntry()) != null) { context.log(PluginHostContext.LOG_INFORMATION, " Deflating " + e.getName() + " (" + e.getSize() + " bytes"); OutputStream fout = null; File f = new File(dir, e.getName()); if (!f.getParentFile().exists() && !f.getParentFile().mkdirs()) throw new IOException("Could not create directory " + f.getParentFile().getAbsolutePath()); fout = new FileOutputStream(f); try { PluginUtil.copyStreams(zin, fout, 65536); } finally { PluginUtil.closeStream(fout); } } } finally { PluginUtil.closeStream(in); } }