public void load() throws IOException { ZipInputStream in = null; try { in = new ZipInputStream(new FileInputStream(this.fileName)); for (ZipEntry zipEntry = in.getNextEntry(); zipEntry != null; zipEntry = in.getNextEntry()) { if (zipEntry.isDirectory()) { continue; } String href = zipEntry.getName(); List<ResourceCallback> filteredCallbacks = findCallbacksFor(href); if (!filteredCallbacks.isEmpty()) { for (ResourceCallback callback : filteredCallbacks) { callback.onLoadResource(href, in); } } } } finally { if (in != null) { in.close(); } this.callbacks.clear(); } }
@Test public void shouldSetTimestampOfEntries() throws IOException { Calendar cal = Calendar.getInstance(); cal.set(1999, SEPTEMBER, 10); long old = getTimeRoundedToSeconds(cal.getTime()); long now = getTimeRoundedToSeconds(new Date()); try (CustomZipOutputStream out = ZipOutputStreams.newOutputStream(output)) { ZipEntry oldAndValid = new ZipEntry("oldAndValid"); oldAndValid.setTime(old); out.putNextEntry(oldAndValid); ZipEntry current = new ZipEntry("current"); current.setTime(now); out.putNextEntry(current); } try (ZipInputStream in = new ZipInputStream(Files.newInputStream(output))) { ZipEntry entry = in.getNextEntry(); assertEquals("oldAndValid", entry.getName()); assertEquals(old, entry.getTime()); entry = in.getNextEntry(); assertEquals("current", entry.getName()); assertEquals(now, entry.getTime()); } }
private List<String> getClasses() throws IOException { if (zipClasses.size() != 0) { return zipClasses; } List<String> classNames = new ArrayList<String>(); ZipInputStream zip = null; try { zip = new ZipInputStream(new FileInputStream("plugins/DDCustomPlugin.jar")); } catch (FileNotFoundException e) { throw new IOException(); } for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) if (entry.getName().endsWith(".class") && !entry.isDirectory()) { // This ZipEntry represents a class. Now, what class does it represent? StringBuilder className = new StringBuilder(); for (String part : entry.getName().split("/")) { if (className.length() != 0) className.append("."); className.append(part); if (part.endsWith(".class")) className.setLength(className.length() - ".class".length()); } if (className.toString().indexOf("$") == -1) classNames.add(className.toString()); } zipClasses = classNames; return classNames; }
private void unpackZip(File tmpZipFile) throws IOException { ZipInputStream zis = new ZipInputStream(new FileInputStream(tmpZipFile)); // get the zipped file list entry ZipEntry ze = zis.getNextEntry(); while (ze != null) { if (ze.isDirectory()) { ze = zis.getNextEntry(); continue; } String fileName = stripLeadingPath(ze.getName()); File newFile = new File(outputDirectory + File.separator + fileName); new File(newFile.getParent()).mkdirs(); FileOutputStream fos = new FileOutputStream(newFile); IOUtils.copy(zis, fos); fos.close(); ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); }
public ZipRepository(final InputStream in, final MimeRegistry mimeRegistry) throws IOException { this(mimeRegistry); final ZipInputStream zipIn = new ZipInputStream(in); try { ZipEntry nextEntry = zipIn.getNextEntry(); while (nextEntry != null) { final String[] buildName = RepositoryUtilities.splitPath(nextEntry.getName(), "/"); if (nextEntry.isDirectory()) { root.updateDirectoryEntry(buildName, 0, nextEntry); } else { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final Deflater def = new Deflater(nextEntry.getMethod()); try { final DeflaterOutputStream dos = new DeflaterOutputStream(bos, def); try { IOUtils.getInstance().copyStreams(zipIn, dos); dos.flush(); } finally { dos.close(); } } finally { def.end(); } root.updateEntry(buildName, 0, nextEntry, bos.toByteArray()); } zipIn.closeEntry(); nextEntry = zipIn.getNextEntry(); } } finally { zipIn.close(); } }
/** Разархивация файла с БД */ private void dearchiveDB() { String zipFile = RES_ABS_PATH + File.separator + DB_ARCHIVE_NAME; String outputFolder = RES_ABS_PATH; try { ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile)); ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); File newFile = new File(outputFolder + File.separator + fileName); System.out.println("File unzip : " + newFile.getAbsoluteFile()); new File(newFile.getParent()).mkdirs(); FileOutputStream fos = new FileOutputStream(newFile); int len; byte[] buffer = new byte[1024]; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); System.out.println("File unziped"); } catch (IOException ex) { ex.printStackTrace(); } }
/** * Loads a factorisation machine from a compressed, human readable file. * * @param in input * @return factorisation machine * @throws IOException when I/O error */ public static FM load(InputStream in) throws IOException { int N; int K; double b; DoubleMatrix1D w; DoubleMatrix2D m; try (ZipInputStream zip = new ZipInputStream(in)) { zip.getNextEntry(); BufferedReader reader = new BufferedReader(new InputStreamReader(zip)); N = parseInt(reader.readLine()); K = parseInt(reader.readLine()); zip.closeEntry(); zip.getNextEntry(); reader = new BufferedReader(new InputStreamReader(zip)); b = parseDouble(reader.readLine()); zip.closeEntry(); zip.getNextEntry(); w = loadDenseDoubleMatrix1D(zip, N); zip.closeEntry(); zip.getNextEntry(); m = loadDenseDoubleMatrix2D(zip, N, K); zip.closeEntry(); } return new FM(b, w, m); }
/** * Add the entries from the supplied {@link ZipInputStream} to this archive instance. * * @param zipStream The zip stream. * @return This archive instance. * @throws IOException Error reading zip stream. */ public Archive addEntries(ZipInputStream zipStream) throws IOException { AssertArgument.isNotNull(zipStream, "zipStream"); try { ZipEntry zipEntry = zipStream.getNextEntry(); ByteArrayOutputStream outByteStream = new ByteArrayOutputStream(); byte[] byteReadBuffer = new byte[512]; int byteReadCount; while (zipEntry != null) { if (zipEntry.isDirectory()) { addEntry(zipEntry.getName(), (byte[]) null); } else { while ((byteReadCount = zipStream.read(byteReadBuffer)) != -1) { outByteStream.write(byteReadBuffer, 0, byteReadCount); } addEntry(zipEntry.getName(), outByteStream.toByteArray()); outByteStream.reset(); } zipEntry = zipStream.getNextEntry(); } } finally { try { zipStream.close(); } catch (IOException e) { logger.debug("Unexpected error closing EDI Mapping Model Zip stream.", e); } } return this; }
private List<ComponentDeclaration> getDeclaredComponents(File jarFile) throws IOException { ZipInputStream zis = new ZipInputStream(new FileInputStream(jarFile)); List<ComponentDeclaration> componentDeclarations = null; List<ComponentDeclaration> componentOverrideDeclarations = null; try { for (ZipEntry entry = zis.getNextEntry(); entry != null && (componentDeclarations == null || componentOverrideDeclarations == null); entry = zis.getNextEntry()) { if (entry.getName().equals(ComponentAnnotationLoader.COMPONENT_LIST)) { componentDeclarations = this.jarLoader.getDeclaredComponents(zis); } else if (entry.getName().equals(ComponentAnnotationLoader.COMPONENT_OVERRIDE_LIST)) { componentOverrideDeclarations = this.jarLoader.getDeclaredComponents(zis); } } } finally { zis.close(); } // Merge all overrides found with a priority of 0. This is purely for backward compatibility // since the // override files is now deprecated. if (componentOverrideDeclarations != null) { if (componentDeclarations == null) { componentDeclarations = new ArrayList<ComponentDeclaration>(); } for (ComponentDeclaration componentOverrideDeclaration : componentOverrideDeclarations) { componentDeclarations.add( new ComponentDeclaration(componentOverrideDeclaration.getImplementationClassName(), 0)); } } return componentDeclarations; }
protected void init() throws IOException { for (int i = 0; i < urls.length; i++) { Set<String> files = new HashSet<String>(500); URLConnection c = urls[i].openConnection(); ByteArrayOutputStream baos = new ByteArrayOutputStream(20000); ZipInputStream zis = new ZipInputStream(c.getInputStream()); ZipEntry ze = zis.getNextEntry(); while (ze != null) { int start = baos.size(); startPosition.put(ze.getName(), start); RepositoryHelp.streamCopy(zis, baos, false); int end = baos.size(); lengths.put(ze.getName(), end - start); files.add(ze.getName()); ze = zis.getNextEntry(); } zis.close(); baos.close(); byte[] theFile = baos.toByteArray(); for (String name : files) { memoryMappedFiles.put(name, theFile); } log("mapped file: " + urls[i]); } }
public void parse( InputStream stream, ContentHandler baseHandler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { // As we don't know which of the metadata or the content // we'll hit first, catch the endDocument call initially EndDocumentShieldingContentHandler handler = new EndDocumentShieldingContentHandler(baseHandler); // Process the file in turn ZipInputStream zip = new ZipInputStream(stream); ZipEntry entry = zip.getNextEntry(); while (entry != null) { if (entry.getName().equals("mimetype")) { String type = IOUtils.toString(zip, "UTF-8"); metadata.set(Metadata.CONTENT_TYPE, type); } else if (entry.getName().equals("meta.xml")) { meta.parse(zip, new DefaultHandler(), metadata, context); } else if (entry.getName().endsWith("content.xml")) { content.parse(zip, handler, metadata, context); } entry = zip.getNextEntry(); } // Only now call the end document if (handler.getEndDocumentWasCalled()) { handler.reallyEndDocument(); } }
private void chargerPresentation(ZipInputStream projectZip) throws ParserConfigurationException, SAXException, IOException, FichierException { ZipEntry zipEntry = projectZip.getNextEntry(); while (zipEntry != null && !this.presentationTrouve) { DataInputStream data = new DataInputStream(new BufferedInputStream(projectZip)); if (zipEntry.getName().equals("Presentation.xml")) { this.presentationTrouve = true; this.print(Application.getApplication().getTraduction("SAX_initialisation")); // preparation du parsing SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxparser = factory.newSAXParser(); this.print(Application.getApplication().getTraduction("presentation_parsing")); PresentationHandler handler = new PresentationHandler(); saxparser.parse(data, handler); this.print(Application.getApplication().getTraduction("Presentation_charge")); } else { zipEntry = projectZip.getNextEntry(); } } projectZip.close(); if (!this.presentationTrouve) { throw new FichierException("Presentation"); } }
public void testCryptRoundtrip() throws Exception { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); ZipOutputStream zipOut = new ZipOutputStream(byteOut); ZipEntry dataEntry = new ZipEntry("Data"); dataEntry.setTime(1000000L); zipOut.putNextEntry(dataEntry); DataOutputStream out = new DataOutputStream(zipOut); for (int i = 0; i < 100; i++) { out.writeInt(i); out.writeUTF(" "); } out.close(); byte[] bytes = byteOut.toByteArray(); ZipInputStream zipIn = new ZipInputStream(new ByteArrayInputStream(bytes)); ZipEntry entry = zipIn.getNextEntry(); while (entry != null && !entry.getName().equals("Data")) { entry = zipIn.getNextEntry(); } if (entry == null) fail("Could not find the data entry"); DataInputStream in = new DataInputStream(zipIn); for (int i = 0; i < 100; i++) { assertEquals(i, in.readInt()); assertEquals("Expected an empty string here.", in.readUTF(), " "); } in.close(); }
/** * Extracts given zip to given location * * @param zipLocation - the location of the zip to be extracted * @param outputLocation - location to extract to */ public static void extractZipTo(String zipLocation, String outputLocation) { ZipInputStream zipinputstream = null; try { byte[] buf = new byte[1024]; zipinputstream = new ZipInputStream(new FileInputStream(zipLocation)); ZipEntry zipentry = zipinputstream.getNextEntry(); while (zipentry != null) { String entryName = zipentry.getName(); int n; if (!zipentry.isDirectory() && !entryName.equalsIgnoreCase("minecraft") && !entryName.equalsIgnoreCase(".minecraft") && !entryName.equalsIgnoreCase("instMods")) { new File(outputLocation + File.separator + entryName).getParentFile().mkdirs(); FileOutputStream fileoutputstream = new FileOutputStream(outputLocation + File.separator + entryName); while ((n = zipinputstream.read(buf, 0, 1024)) > -1) { fileoutputstream.write(buf, 0, n); } fileoutputstream.close(); } zipinputstream.closeEntry(); zipentry = zipinputstream.getNextEntry(); } } catch (Exception e) { Logger.logError(e.getMessage(), e); backupExtract(zipLocation, outputLocation); } finally { try { zipinputstream.close(); } catch (IOException e) { } } }
/** * Unzip the specified input stream into a folder. * * @param inputStream the input stream to unzip (this must contain zip contents) * @param destination the destination folder * @see #unpack(File) */ public static void unpack(InputStream inputStream, Folder destination) { Assert.notNull(inputStream, "InputStream must not be null"); Assert.notNull(destination, "Destination must not be null"); destination.createIfMissing(); ZipInputStream zip = new ZipInputStream(new BufferedInputStream(inputStream)); try { InputStream noCloseZip = new NoCloseInputStream(zip); ZipEntry entry = zip.getNextEntry(); while (entry != null) { if (entry.isDirectory()) { destination.getFolder(entry.getName()).createIfMissing(); } else { destination.getFile(entry.getName()).getContent().write(noCloseZip); } entry = zip.getNextEntry(); } } catch (IOException e) { throw new ResourceException(e); } finally { try { zip.close(); } catch (IOException e) { } } }
@Override public void internalParse(InputStream input) { try { try { Thread.sleep(4000); } catch (Throwable t) { } SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); saxParserFactory.setValidating(false); SAXParser saxParser = saxParserFactory.newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setFeature("http://xml.org/sax/features/validation", false); xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); ZipInputStream zis = new ZipInputStream(input); ZipEntry ze = zis.getNextEntry(); while (ze != null && !ze.getName().equals("content.xml")) { ze = zis.getNextEntry(); } OpenOfficeContentHandler contentHandler = new OpenOfficeContentHandler(); xmlReader.setContentHandler(contentHandler); try { xmlReader.parse(new InputSource(zis)); } finally { zis.close(); } content.append(StringUtil.writeToString(new StringReader(contentHandler.getContent()))); } catch (Exception e) { log.warn("Failed to extract OpenOffice text content", e); } }
public void addFromZip(String zipFilename) throws IOException { byte[] buf = new byte[1024]; ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFilename)); try { ZipEntry entry = zin.getNextEntry(); while (entry != null) { String name = entry.getName(); names.add(name); // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(name)); // Transfer bytes from the ZIP file to the output file int len; while ((len = zin.read(buf)) > 0) { out.write(buf, 0, len); } entry = zin.getNextEntry(); } } finally { zin.close(); } }
public static void unzip(byte[] content, final File destinationDir) throws IOException { byte[] buffer = new byte[BUFFER_SIZE]; ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(content)); ZipEntry zipEntry = zis.getNextEntry(); while (zipEntry != null) { String fileName = zipEntry.getName(); File file = new File(destinationDir + File.separator + fileName); if (zipEntry.isDirectory()) { ensureExistingDirectory(file); } else { ensureExistingDirectory(file.getParentFile()); FileOutputStream fos = new FileOutputStream(file); try { int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } } finally { closeQuietly(fos); } } zipEntry = zis.getNextEntry(); } zis.closeEntry(); zis.close(); }
protected void unzip(String pathToFile, String outputFolder) { System.out.println("Start unzipping: " + pathToFile + " to " + outputFolder); String pathToUnzip; if (outputFolder.equals(pathManager.getPlatformFolderInAndroidSdk())) { pathToUnzip = outputFolder; } else { pathToUnzip = outputFolder + "/" + FileUtil.getNameWithoutExtension(new File(pathToFile)); } if (new File(pathToUnzip).listFiles() != null) { System.out.println("File was already unzipped: " + pathToFile); return; } try { byte[] buf = new byte[1024]; ZipInputStream zipinputstream = null; ZipEntry zipentry; zipinputstream = new ZipInputStream(new FileInputStream(pathToFile)); zipentry = zipinputstream.getNextEntry(); try { while (zipentry != null) { String entryName = zipentry.getName(); int n; File outputFile = new File(outputFolder + "/" + entryName); if (zipentry.isDirectory()) { outputFile.mkdirs(); zipinputstream.closeEntry(); zipentry = zipinputstream.getNextEntry(); continue; } else { File parentFile = outputFile.getParentFile(); if (parentFile != null && !parentFile.exists()) { parentFile.mkdirs(); } outputFile.createNewFile(); } FileOutputStream fileoutputstream = new FileOutputStream(outputFile); try { while ((n = zipinputstream.read(buf, 0, 1024)) > -1) { fileoutputstream.write(buf, 0, n); } } finally { fileoutputstream.close(); } zipinputstream.closeEntry(); zipentry = zipinputstream.getNextEntry(); } zipinputstream.close(); } catch (IOException e) { System.err.println("Entry name: " + zipentry.getName()); e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } System.out.println("Finish unzipping: " + pathToFile + " to " + outputFolder); }
public void unzip(File from, String to) throws FITTESTException { try { FileInputStream fis = new FileInputStream(from); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis, BUFFER)); ZipEntry entry = zis.getNextEntry(); while (entry != null) { int count; byte data[] = new byte[BUFFER]; File outFile = null; if (entry.isDirectory()) { outFile = new File(to, entry.getName()); outFile.mkdirs(); } else { outFile = new File(to, entry.getName()); FileOutputStream fos = new FileOutputStream(outFile); BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER); while ((count = zis.read(data, 0, BUFFER)) != -1) { bos.write(data, 0, count); } bos.flush(); bos.close(); } zis.closeEntry(); entry = zis.getNextEntry(); } zis.close(); } catch (Exception e) { throw new FITTESTException(e.getMessage()); } }
static Hashtable<String, PkgEntry> readJarFile(InputStream in) { Hashtable<String, PkgEntry> tbl = new Hashtable<String, PkgEntry>(); try { ZipInputStream zis = new ZipInputStream(in); ZipEntry ze = zis.getNextEntry(); while (ze != null) { String zname = ze.getName(); if (zname.endsWith(".class")) { String pkgname = getPackageName(zname); if (!tbl.containsKey(pkgname)) { long csize = getCompressedSize(zis); tbl.put(pkgname, new PkgEntry(ze.getSize(), csize)); } else { PkgEntry pe = tbl.get(pkgname); long csize = getCompressedSize(zis); pe.addSizes(ze.getSize(), csize); tbl.put(pkgname, pe); } } ze = zis.getNextEntry(); } } catch (ZipException ex) { Logger.getLogger(JarAnalyzer.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(JarAnalyzer.class.getName()).log(Level.SEVERE, null, ex); } return tbl; }
@Test public void packingALargeFileShouldGenerateTheSameOutputWhenOverwritingAsWhenAppending() throws IOException { File reference = File.createTempFile("reference", ".zip"); String packageName = getClass().getPackage().getName().replace(".", "/"); URL sample = Resources.getResource(packageName + "/macbeth.properties"); byte[] input = Resources.toByteArray(sample); try (CustomZipOutputStream out = ZipOutputStreams.newOutputStream(output, OVERWRITE_EXISTING); ZipOutputStream ref = new ZipOutputStream(new FileOutputStream(reference))) { CustomZipEntry entry = new CustomZipEntry("macbeth.properties"); entry.setTime(System.currentTimeMillis()); out.putNextEntry(entry); ref.putNextEntry(entry); out.write(input); ref.write(input); } byte[] seen = Files.readAllBytes(output); byte[] expected = Files.readAllBytes(reference.toPath()); // Make sure the output is valid. try (ZipInputStream in = new ZipInputStream(Files.newInputStream(output))) { ZipEntry entry = in.getNextEntry(); assertEquals("macbeth.properties", entry.getName()); assertNull(in.getNextEntry()); } assertArrayEquals(expected, seen); }
/** * Parse a repository document. * * @param url * @throws IOException * @throws XmlPullParserException * @throws Exception */ void parseDocument(URL url) throws IOException, XmlPullParserException, Exception { if (!visited.contains(url)) { visited.add(url); try { System.out.println("Visiting: " + url); InputStream in = null; if (url.getPath().endsWith(".zip")) { ZipInputStream zin = new ZipInputStream(url.openStream()); ZipEntry entry = zin.getNextEntry(); while (entry != null) { if (entry.getName().equals("repository.xml")) { in = zin; break; } entry = zin.getNextEntry(); } } else { in = url.openStream(); } Reader reader = new InputStreamReader(in); XmlPullParser parser = new KXmlParser(); parser.setInput(reader); parseRepository(parser); } catch (MalformedURLException e) { System.out.println("Cannot create connection to url"); } } }
/** * Loads a matrix from a compressed input stream. * * @param <U> type of the users * @param <I> type of the items * @param in input stream * @param uIndex fast user index * @param iIndex fast item index * @param uParser user type parser * @param iParser item type parser * @return a factorization * @throws IOException when IO error */ public static <U, I> Factorization<U, I> load( InputStream in, FastUserIndex<U> uIndex, FastItemIndex<I> iIndex, Parser<U> uParser, Parser<I> iParser) throws IOException { int K; DenseDoubleMatrix2D userMatrix; DenseDoubleMatrix2D itemMatrix; try (ZipInputStream zip = new ZipInputStream(in)) { zip.getNextEntry(); BufferedReader reader = new BufferedReader(new InputStreamReader(zip)); int numUsers = ip.parse(reader.readLine()); int numItems = ip.parse(reader.readLine()); K = ip.parse(reader.readLine()); zip.closeEntry(); zip.getNextEntry(); userMatrix = loadDenseDoubleMatrix2D(zip, numUsers, K); zip.closeEntry(); zip.getNextEntry(); itemMatrix = loadDenseDoubleMatrix2D(zip, numItems, K); zip.closeEntry(); } return new Factorization<>(uIndex, iIndex, userMatrix, itemMatrix, K); }
/** * Unzip the data directory to the destination * * @throws IOException * @throws FileNotFoundException */ private void unzipData(File destination) throws IOException, FileNotFoundException { byte[] buffer = new byte[1024]; // get the zip file content ZipInputStream zis = new ZipInputStream(FileLocator.openStream(bundle, new Path(EXECUTION_PLAN_DATA_ZIP), true)); // Get the zipped file list entry ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); File newFile = new File(destination, fileName); // Create all directories if they do not already exist new File(newFile.getParent()).mkdirs(); if (ze.isDirectory()) { newFile.mkdirs(); } else { // Read out the file FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); } ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); }
/** * Unzip all the projects contained by the zip. * * @param bundleName the bundle name * @param zipLocation the zip location inside of the bundle * @param monitor the progress monitor * @throws IOException if there is an issue copying a file from the zip * @throws CoreException if there is an issue creating one of the projects */ public static void unzipAllProjects( String bundleName, String zipLocation, IProgressMonitor monitor) throws IOException, CoreException { final URL interpreterZipUrl = FileLocator.find(Platform.getBundle(bundleName), new Path(zipLocation), null); final ZipInputStream zipFileStream = new ZipInputStream(interpreterZipUrl.openStream()); ZipEntry zipEntry = zipFileStream.getNextEntry(); Set<IProject> projects = new HashSet<IProject>(); while (zipEntry != null) { String projectName = zipEntry.getName().split("/")[0]; IProject project = createProject(projectName, monitor); projects.add(project); final File file = new File( project.getLocation().toString(), zipEntry .getName() .replaceFirst(projectName + "/", "")); // $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ if (!zipEntry.isDirectory()) { /* * Copy files (and make sure parent directory exist) */ final File parentFile = file.getParentFile(); if (null != parentFile && !parentFile.exists()) { parentFile.mkdirs(); } OutputStream os = null; try { os = new FileOutputStream(file); final int bufferSize = 102400; final byte[] buffer = new byte[bufferSize]; while (true) { final int len = zipFileStream.read(buffer); if (zipFileStream.available() == 0) { break; } os.write(buffer, 0, len); } } finally { if (null != os) { os.close(); } } } zipFileStream.closeEntry(); zipEntry = zipFileStream.getNextEntry(); } ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, monitor); }
public static void extractLegacyEditorResources() { try { String destParent = applicationDataDir() + ".ApplicationData" + File.separator; File appHome = new File(destParent); if (!appHome.exists()) { if (!appHome.mkdirs()) { SEAGridDialogHelper.showExceptionDialogAndWait( new Exception("Cannot Create Application Data Dir"), "Cannot Create Application Data Dir", null, "Cannot Create Application Data Dir"); } } byte[] buf = new byte[1024]; ZipInputStream zipinputstream; ZipEntry zipentry; zipinputstream = new ZipInputStream( SEAGridDesktop.class.getClassLoader().getResourceAsStream("legacy.editors.zip")); zipentry = zipinputstream.getNextEntry(); if (zipentry == null) { SEAGridDialogHelper.showExceptionDialogAndWait( new Exception("Cannot Read Application Resources"), "Cannot Read Application Resources", null, "Cannot Read Application Resources"); } else { while (zipentry != null) { // for each entry to be extracted String entryName = destParent + zipentry.getName(); entryName = entryName.replace('/', File.separatorChar); entryName = entryName.replace('\\', File.separatorChar); logger.info("entryname " + entryName); int n; FileOutputStream fileoutputstream; File newFile = new File(entryName); if (zipentry.isDirectory()) { if (!newFile.mkdirs()) { break; } zipentry = zipinputstream.getNextEntry(); continue; } 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 (Exception e) { e.printStackTrace(); logger.error(e.getMessage(), e); } }
String nextEntry(String extension) throws IOException { entry = zipInputStream.getNextEntry(); while (entry != null && !entry.getName().endsWith(extension)) { entry = zipInputStream.getNextEntry(); } if (entry != null) { return entry.getName(); } return null; }
@NotNull protected Map<String, EntryInfo> initEntries() { synchronized (lock) { Map<String, EntryInfo> map = myRelPathsToEntries.get(); if (map == null) { final ZipInputStream zip = getZip(); map = new HashMap<String, EntryInfo>(); if (zip != null) { map.put("", new EntryInfo("", null, true, new byte[0])); try { ZipEntry entry = zip.getNextEntry(); while (entry != null) { final String name = entry.getName(); final boolean isDirectory = name.endsWith("/"); if (entry.getExtra() == null) { byte[] cont = new byte[(int) entry.getSize()]; ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); InputStream stream = getZip(); if (stream != null) { int tmp; if ((tmp = stream.read(cont)) == entry.getSize()) { byteArray.write(cont, 0, tmp); entry.setExtra(byteArray.toByteArray()); } else { int readFromIS = tmp; if (tmp < entry.getSize()) { byteArray.write(cont, 0, tmp); while (((tmp = stream.read(cont)) != -1) && (tmp + readFromIS <= entry.getSize())) { byteArray.write(cont, 0, tmp); readFromIS += tmp; } entry.setExtra(byteArray.toByteArray()); } } } } getOrCreate( isDirectory ? name.substring(0, name.length() - 1) : name, isDirectory, map, entry.getExtra()); entry = zip.getNextEntry(); } } catch (IOException e) { e.printStackTrace(); } myRelPathsToEntries = new SoftReference<Map<String, EntryInfo>>(map); } } return map; } }
/** * Wow, this is hard to understand... * * @param inZip -- The Zip InputStream entries have paths buried in them e.g., * data/subdir/file.txt * @param inDest -- The destination folder, e.g. /opt/appserver/webapp/ROOT * @param inPathSegment -- A path segment that might exist in the Zip entries, e.g. /data or data * @param inNewPathSegment -- A new path segment we want to use, e.g. /backup to make * backup/subdir/file.txt * @throws IOException */ public List unzip(InputStream inZip, File inDest, String inCutOffLeadingPath) throws IOException { ZipInputStream unzip = new ZipInputStream(inZip); ZipEntry entry = unzip.getNextEntry(); List unzippedFiles = new ArrayList(); while (entry != null) { String entryPath = composeEntryPath(entry.getName(), inCutOffLeadingPath); if (fieldFindFileName != null) { if (!PathUtilities.match(entry.getName(), fieldFindFileName)) // TODO // : // Handle // leading // / { entry = unzip.getNextEntry(); continue; } } if (entryPath.contains("__MACOSX")) { entry = unzip.getNextEntry(); continue; } // int zipeSize = (int) entry.getSize(); if (!entry.isDirectory()) { File ufile = null; if (inDest != null) { ufile = new File(inDest, entryPath); } else { ufile = new File(entryPath); } ufile.getParentFile().mkdirs(); if (ufile.exists() && !ufile.delete()) { getWindowsUtil().delete(ufile); } FileOutputStream tout = new FileOutputStream(ufile); try { filler.fill(unzip, tout); } finally { FileUtils.safeClose(tout); } ufile.setLastModified(entry.getTime()); if (log.isDebugEnabled()) { log.debug(ufile.getAbsolutePath()); } unzippedFiles.add(ufile); if (isExitOnFirstFind()) { return unzippedFiles; } } entry = unzip.getNextEntry(); } return unzippedFiles; }