use of java.util.zip.ZipFile in project deeplearning4j by deeplearning4j.
the class ClassPathResource method getFile.
/**
* Returns requested ClassPathResource as File object
*
* Please note: if this method called from compiled jar, temporary file will be created to provide File access
*
* @return File requested at constructor call
* @throws FileNotFoundException
*/
public File getFile() throws FileNotFoundException {
URL url = this.getUrl();
if (isJarURL(url)) {
/*
This is actually request for file, that's packed into jar. Probably the current one, but that doesn't matters.
*/
try {
url = extractActualUrl(url);
File file = File.createTempFile("datavec_temp", "file");
file.deleteOnExit();
ZipFile zipFile = new ZipFile(url.getFile());
ZipEntry entry = zipFile.getEntry(this.resourceName);
if (entry == null) {
if (this.resourceName.startsWith("/")) {
entry = zipFile.getEntry(this.resourceName.replaceFirst("/", ""));
if (entry == null) {
throw new FileNotFoundException("Resource " + this.resourceName + " not found");
}
} else
throw new FileNotFoundException("Resource " + this.resourceName + " not found");
}
long size = entry.getSize();
InputStream stream = zipFile.getInputStream(entry);
FileOutputStream outputStream = new FileOutputStream(file);
byte[] array = new byte[1024];
int rd = 0;
long bytesRead = 0;
do {
rd = stream.read(array);
outputStream.write(array, 0, rd);
bytesRead += rd;
} while (bytesRead < size);
outputStream.flush();
outputStream.close();
stream.close();
zipFile.close();
return file;
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
try {
URI uri = new URI(url.toString().replaceAll(" ", "%20"));
return new File(uri.getSchemeSpecificPart());
} catch (URISyntaxException e) {
return new File(url.getFile());
}
}
}
use of java.util.zip.ZipFile in project deeplearning4j by deeplearning4j.
the class ClassPathResource method getInputStream.
/**
* Returns requested ClassPathResource as InputStream object
*
* @return File requested at constructor call
* @throws FileNotFoundException
*/
public InputStream getInputStream() throws FileNotFoundException {
URL url = this.getUrl();
if (isJarURL(url)) {
try {
url = extractActualUrl(url);
ZipFile zipFile = new ZipFile(url.getFile());
ZipEntry entry = zipFile.getEntry(this.resourceName);
if (entry == null) {
if (this.resourceName.startsWith("/")) {
entry = zipFile.getEntry(this.resourceName.replaceFirst("/", ""));
if (entry == null) {
throw new FileNotFoundException("Resource " + this.resourceName + " not found");
}
} else
throw new FileNotFoundException("Resource " + this.resourceName + " not found");
}
InputStream stream = zipFile.getInputStream(entry);
return stream;
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
File srcFile = this.getFile();
return new FileInputStream(srcFile);
}
}
use of java.util.zip.ZipFile in project che by eclipse.
the class Util method getJdkLevel.
/**
* Get the jdk level of this root.
* The value can be:
* <ul>
* <li>major<<16 + minor : see predefined constants on ClassFileConstants </li>
* <li><code>0</null> if the root is a source package fragment root or if a Java model exception occured</li>
* </ul>
* Returns the jdk level
*/
public static long getJdkLevel(Object targetLibrary) {
try {
ClassFileReader reader = null;
if (targetLibrary instanceof IFolder) {
// only internal classfolders are allowed
IFile classFile = findFirstClassFile((IFolder) targetLibrary);
if (classFile != null)
reader = Util.newClassFileReader(classFile);
} else {
// root is a jar file or a zip file
ZipFile jar = null;
try {
IPath path = null;
if (targetLibrary instanceof IResource) {
path = ((IResource) targetLibrary).getFullPath();
} else if (targetLibrary instanceof File) {
File f = (File) targetLibrary;
if (!f.isDirectory()) {
path = new Path(((File) targetLibrary).getPath());
}
}
if (path != null) {
jar = JavaModelManager.getJavaModelManager().getZipFile(path);
for (Enumeration e = jar.entries(); e.hasMoreElements(); ) {
ZipEntry member = (ZipEntry) e.nextElement();
String entryName = member.getName();
if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(entryName)) {
reader = ClassFileReader.read(jar, entryName);
break;
}
}
}
} catch (CoreException e) {
// ignore
} finally {
JavaModelManager.getJavaModelManager().closeZipFile(jar);
}
}
if (reader != null) {
return reader.getVersion();
}
} catch (CoreException e) {
// ignore
} catch (ClassFormatException e) {
// ignore
} catch (IOException e) {
// ignore
}
return 0;
}
use of java.util.zip.ZipFile in project che by eclipse.
the class InstallExtension method getExtensionFromFile.
private static Extension getExtensionFromFile(Path zipPath) throws IOException {
try (ZipFile zipFile = new ZipFile(zipPath.toString())) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
ZipEntry gwtXmlEntry = null;
ZipEntry pomEntry = null;
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (!entry.isDirectory()) {
if (entry.getName().endsWith(GwtXmlUtils.GWT_MODULE_XML_SUFFIX)) {
gwtXmlEntry = entry;
} else if (entry.getName().endsWith("pom.xml")) {
pomEntry = entry;
}
}
// have both entries
if (pomEntry != null && gwtXmlEntry != null) {
break;
}
}
// TODO consider Codenvy extension validator
if (pomEntry == null) {
return null;
}
String gwtModuleName = null;
if (gwtXmlEntry != null) {
gwtModuleName = gwtXmlEntry.getName().replace(File.separatorChar, '.');
gwtModuleName = gwtModuleName.substring(0, gwtModuleName.length() - GwtXmlUtils.GWT_MODULE_XML_SUFFIX.length());
}
Model pom = Model.readFrom(zipFile.getInputStream(pomEntry));
return new Extension(gwtModuleName, MavenUtils.getGroupId(pom), pom.getArtifactId(), MavenUtils.getVersion(pom));
}
}
use of java.util.zip.ZipFile in project che by eclipse.
the class ClasspathJar method readPackages.
private SimpleSet readPackages() {
try {
if (this.zipFile == null) {
if (org.eclipse.jdt.internal.core.JavaModelManager.ZIP_ACCESS_VERBOSE) {
System.out.println("(" + Thread.currentThread() + ") [ClasspathJar.isPackage(String)] Creating ZipFile on " + this.zipFilename);
//$NON-NLS-1$ //$NON-NLS-2$
}
this.zipFile = new ZipFile(this.zipFilename);
this.closeZipFileAtEnd = true;
}
return findPackageSet(this);
} catch (Exception e) {
// assume for this build the zipFile is empty
return new SimpleSet();
}
}
Aggregations