use of java.util.zip.ZipFile in project druid by druid-io.
the class CompressionUtils method unzip.
/**
* Unzip the pulled file to an output directory. This is only expected to work on zips with lone files, and is not intended for zips with directory structures.
*
* @param pulledFile The file to unzip
* @param outDir The directory to store the contents of the file.
*
* @return a FileCopyResult of the files which were written to disk
*
* @throws IOException
*/
public static FileUtils.FileCopyResult unzip(final File pulledFile, final File outDir) throws IOException {
if (!(outDir.exists() && outDir.isDirectory())) {
throw new ISE("outDir[%s] must exist and be a directory", outDir);
}
log.info("Unzipping file[%s] to [%s]", pulledFile, outDir);
final FileUtils.FileCopyResult result = new FileUtils.FileCopyResult();
try (final ZipFile zipFile = new ZipFile(pulledFile)) {
final Enumeration<? extends ZipEntry> enumeration = zipFile.entries();
while (enumeration.hasMoreElements()) {
final ZipEntry entry = enumeration.nextElement();
result.addFiles(FileUtils.retryCopy(new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return new BufferedInputStream(zipFile.getInputStream(entry));
}
}, new File(outDir, entry.getName()), FileUtils.IS_EXCEPTION, DEFAULT_RETRY_COUNT).getFiles());
}
}
return result;
}
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 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();
}
}
use of java.util.zip.ZipFile in project che by eclipse.
the class JavaNavigation method getContent.
public ClassContent getContent(IJavaProject project, int rootId, String path) throws CoreException {
IPackageFragmentRoot root = getPackageFragmentRoot(project, rootId);
if (root == null) {
return null;
}
if (path.startsWith("/")) {
//non java file
if (root instanceof JarPackageFragmentRoot) {
JarPackageFragmentRoot jarPackageFragmentRoot = (JarPackageFragmentRoot) root;
ZipFile jar = null;
try {
jar = jarPackageFragmentRoot.getJar();
ZipEntry entry = jar.getEntry(path.substring(1));
if (entry != null) {
try (InputStream stream = jar.getInputStream(entry)) {
return createContent(IoUtil.readStream(stream), false);
} catch (IOException e) {
LOG.error("Can't read file content: " + entry.getName(), e);
}
}
} finally {
if (jar != null) {
JavaModelManager.getJavaModelManager().closeZipFile(jar);
}
}
}
Object[] resources = root.getNonJavaResources();
for (Object resource : resources) {
if (resource instanceof JarEntryFile) {
JarEntryFile file = (JarEntryFile) resource;
if (file.getFullPath().toOSString().equals(path)) {
return readFileContent(file);
}
}
if (resource instanceof JarEntryDirectory) {
JarEntryDirectory directory = (JarEntryDirectory) resource;
JarEntryFile file = findJarFile(directory, path);
if (file != null) {
return readFileContent(file);
}
}
}
} else {
return getContent(project, path);
}
return null;
}
use of java.util.zip.ZipFile in project buck by facebook.
the class JarDirectoryStepHelper method createManifest.
private static Manifest createManifest(ProjectFilesystem filesystem, ImmutableSortedSet<Path> entriesToJar, Optional<String> mainClass, Optional<Path> manifestFile, boolean mergeManifests) throws IOException {
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
if (mergeManifests) {
for (Path entry : entriesToJar) {
entry = filesystem.getPathForRelativePath(entry);
Manifest readManifest;
if (Files.isDirectory(entry)) {
Path manifestPath = entry.resolve(JarFile.MANIFEST_NAME);
if (!Files.exists(manifestPath)) {
continue;
}
try (InputStream inputStream = Files.newInputStream(manifestPath)) {
readManifest = new Manifest(inputStream);
}
} else {
// Assume a zip or jar file.
try (ZipFile zipFile = new ZipFile(entry.toFile())) {
ZipEntry manifestEntry = zipFile.getEntry(JarFile.MANIFEST_NAME);
if (manifestEntry == null) {
continue;
}
try (InputStream inputStream = zipFile.getInputStream(manifestEntry)) {
readManifest = new Manifest(inputStream);
}
}
}
merge(manifest, readManifest);
}
}
// so that values from the user overwrite values from merged manifests.
if (manifestFile.isPresent()) {
Path path = filesystem.getPathForRelativePath(manifestFile.get());
try (InputStream stream = Files.newInputStream(path)) {
Manifest readManifest = new Manifest(stream);
merge(manifest, readManifest);
}
}
// We may have merged manifests and over-written the user-supplied main class. Add it back.
if (mainClass.isPresent()) {
manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, mainClass.get());
}
return manifest;
}
Aggregations