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 DexJarAnalysisStep method execute.
@Override
public StepExecutionResult execute(ExecutionContext context) throws InterruptedException {
try (ZipFile zf = new ZipFile(filesystem.resolve(dexPath).toFile())) {
ZipEntry classesDexEntry = zf.getEntry("classes.dex");
if (classesDexEntry == null) {
throw new RuntimeException("could not find classes.dex in jar");
}
long uncompressedSize = classesDexEntry.getSize();
if (uncompressedSize == -1) {
throw new RuntimeException("classes.dex size should be known");
}
filesystem.writeContentsToPath(String.format("jar:%s dex:%s", filesystem.getFileSize(dexPath), uncompressedSize), dexMetaPath);
return StepExecutionResult.SUCCESS;
} catch (IOException e) {
context.logError(e, "There was an error in smart dexing step.");
return StepExecutionResult.ERROR;
}
}
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;
}
use of java.util.zip.ZipFile in project buck by facebook.
the class AgentUtil method getJarSignature.
public static String getJarSignature(String packagePath) throws IOException {
Pattern signatureFilePattern = Pattern.compile("META-INF/[A-Z]+\\.SF");
ZipFile packageZip = null;
try {
packageZip = new ZipFile(packagePath);
// For each file in the zip.
for (ZipEntry entry : Collections.list(packageZip.entries())) {
// Ignore non-signature files.
if (!signatureFilePattern.matcher(entry.getName()).matches()) {
continue;
}
BufferedReader sigContents = null;
try {
sigContents = new BufferedReader(new InputStreamReader(packageZip.getInputStream(entry)));
// For each line in the signature file.
while (true) {
String line = sigContents.readLine();
if (line == null || line.equals("")) {
throw new IllegalArgumentException("Failed to find manifest digest in " + entry.getName());
}
String prefix = "SHA1-Digest-Manifest: ";
if (line.startsWith(prefix)) {
return line.substring(prefix.length());
}
}
} finally {
if (sigContents != null) {
sigContents.close();
}
}
}
} finally {
if (packageZip != null) {
packageZip.close();
}
}
throw new IllegalArgumentException("Failed to find signature file.");
}
use of java.util.zip.ZipFile in project buck by facebook.
the class DxAnalysisMain method loadAllClasses.
private static ImmutableMap<String, ClassNode> loadAllClasses(String zipFileName) throws IOException {
ImmutableMap.Builder<String, ClassNode> allClassesBuilder = ImmutableMap.builder();
try (ZipFile inJar = new ZipFile(zipFileName)) {
for (ZipEntry entry : Collections.list(inJar.entries())) {
if (!entry.getName().endsWith(".class")) {
continue;
}
// Skip external libraries.
if (entry.getName().startsWith("junit/") || entry.getName().startsWith("org/junit/") || entry.getName().startsWith("com/google/common/")) {
continue;
}
byte[] rawClass = ByteStreams.toByteArray(inJar.getInputStream(entry));
ClassNode klass = new ClassNode();
new ClassReader(rawClass).accept(klass, ClassReader.EXPAND_FRAMES);
allClassesBuilder.put(klass.name, klass);
}
}
return allClassesBuilder.build();
}
Aggregations