Search in sources :

Example 31 with ZipEntry

use of java.util.zip.ZipEntry 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;
}
Also used : JarEntryDirectory(org.eclipse.jdt.internal.core.JarEntryDirectory) ZipFile(java.util.zip.ZipFile) JarPackageFragmentRoot(org.eclipse.jdt.internal.core.JarPackageFragmentRoot) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) JarEntryFile(org.eclipse.jdt.internal.core.JarEntryFile) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 32 with ZipEntry

use of java.util.zip.ZipEntry in project jetty.project by eclipse.

the class WarURLConnection method substitueManifest.

/**
     * Use PipedOuputStream and PipedInputStream to do the transformation without making
     * a new temporary file ust to replace the manifest.
     * @param newmanifest The new manifest
     * @param rawIn The file input stream or equivalent. not the jar input stream.
     */
public static InputStream substitueManifest(final Manifest newmanifest, final InputStream rawIn) throws IOException {
    final PipedOutputStream pOut = new PipedOutputStream();
    PipedInputStream pIn = new PipedInputStream(pOut);
    Runnable run = new Runnable() {

        public void run() {
            JarInputStream jin = null;
            JarOutputStream dest = null;
            try {
                jin = new JarInputStream(rawIn, false);
                dest = new JarOutputStream(pOut, newmanifest);
                ZipEntry next = jin.getNextEntry();
                while (next != null) {
                    if (next.getName().equalsIgnoreCase(JarFile.MANIFEST_NAME)) {
                        continue;
                    }
                    dest.putNextEntry(next);
                    if (next.getSize() > 0) {
                        IO.copy(jin, dest, next.getSize());
                    }
                    next = jin.getNextJarEntry();
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } finally {
                if (dest != null)
                    IO.close(dest);
                if (jin != null)
                    IO.close(jin);
                IO.close(pOut);
            }
        }
    };
    Thread th = new Thread(run);
    th.start();
    return pIn;
}
Also used : JarInputStream(java.util.jar.JarInputStream) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException)

Example 33 with ZipEntry

use of java.util.zip.ZipEntry 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;
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Example 34 with ZipEntry

use of java.util.zip.ZipEntry in project buck by facebook.

the class HashingDeterministicJarWriter method putEntry.

private void putEntry(ZipOutputStream jar, String fileName) throws IOException {
    ZipEntry entry = new ZipEntry(fileName);
    // We want deterministic JARs, so avoid mtimes.
    entry.setTime(ZipConstants.getFakeTime());
    jar.putNextEntry(entry);
}
Also used : ZipEntry(java.util.zip.ZipEntry)

Example 35 with ZipEntry

use of java.util.zip.ZipEntry 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;
}
Also used : Path(java.nio.file.Path) ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) Manifest(java.util.jar.Manifest)

Aggregations

ZipEntry (java.util.zip.ZipEntry)1367 ZipFile (java.util.zip.ZipFile)479 File (java.io.File)469 IOException (java.io.IOException)361 ZipOutputStream (java.util.zip.ZipOutputStream)321 ZipInputStream (java.util.zip.ZipInputStream)300 InputStream (java.io.InputStream)282 FileOutputStream (java.io.FileOutputStream)278 FileInputStream (java.io.FileInputStream)270 Test (org.junit.Test)124 BufferedInputStream (java.io.BufferedInputStream)122 JarFile (java.util.jar.JarFile)122 BufferedOutputStream (java.io.BufferedOutputStream)99 ByteArrayOutputStream (java.io.ByteArrayOutputStream)97 ArrayList (java.util.ArrayList)84 ByteArrayInputStream (java.io.ByteArrayInputStream)78 OutputStream (java.io.OutputStream)67 JarOutputStream (java.util.jar.JarOutputStream)59 Path (java.nio.file.Path)56 Enumeration (java.util.Enumeration)56