use of java.util.zip.ZipEntry in project cogtool by cogtool.
the class ZipUtil method zipDirectory.
private static void zipDirectory(File dir, String base, ZipOutputStream zout) throws IOException {
// list all
File[] files = dir.listFiles();
base += dir.getName() + "/";
if (files.length > 0) {
// if there are files in this directory, add them (this dir is implicit)
for (File file : files) {
if (file.isDirectory()) {
// recur on directories
zipDirectory(file, base, zout);
} else {
// add files to the stream
zipOneFile(file, base, zout);
}
}
} else {
// we have to add this dir explicitly as an entry
// Build proper entry name
String name = base + dir.getName() + '/';
// Create a ZipEntry
ZipEntry entry = new ZipEntry(name);
entry.setTime(dir.lastModified());
// Put the entry into the zip (actually, this just writes the header)
zout.putNextEntry(entry);
// Close off the entry (actually, this just writes more header info)
zout.closeEntry();
}
}
use of java.util.zip.ZipEntry 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.ZipEntry 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.ZipEntry 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.ZipEntry in project che by eclipse.
the class ZipFileStructureProvider method createContainer.
/**
* Creates a new container zip entry with the specified name, iff
* it has not already been created.
*/
protected void createContainer(IPath pathname) {
if (directoryEntryCache.containsKey(pathname)) {
return;
}
ZipEntry parent;
if (pathname.segmentCount() == 1) {
parent = root;
} else {
parent = (ZipEntry) directoryEntryCache.get(pathname.removeLastSegments(1));
}
ZipEntry newEntry = new ZipEntry(pathname.toString());
directoryEntryCache.put(pathname, newEntry);
addToChildren(parent, newEntry);
}
Aggregations