Search in sources :

Example 46 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project intellij-community by JetBrains.

the class InternalTestDiscoveryListener method zipOutput.

private static void zipOutput(String tracesDirectory) {
    final File[] files = new File(tracesDirectory).listFiles();
    if (files == null) {
        System.out.println("No traces found.");
        return;
    }
    System.out.println("Preparing zip.");
    try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(tracesDirectory + File.separator + "out.zip"))) {
        for (File file : files) {
            ZipUtil.addFileToZip(zipOutputStream, file, "/" + file.getName(), null, null);
        }
        System.out.println("Zip prepared.");
        for (File file : files) {
            FileUtil.delete(file);
        }
    } catch (Throwable ex) {
        ex.printStackTrace();
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 47 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project intellij-community by JetBrains.

the class ZipUtil method update.

/*
   * update an existing jar file. Adds/replace files specified in relpathToFile map
   */
public static void update(InputStream in, OutputStream out, Map<String, File> relpathToFile) throws IOException {
    ZipInputStream zis = new ZipInputStream(in);
    ZipOutputStream zos = new ZipOutputStream(out);
    try {
        // put the old entries first, replace if necessary
        ZipEntry e;
        while ((e = zis.getNextEntry()) != null) {
            String name = e.getName();
            if (!relpathToFile.containsKey(name)) {
                // copy the old stuff
                // do our own compression
                ZipEntry e2 = new ZipEntry(name);
                e2.setMethod(e.getMethod());
                e2.setTime(e.getTime());
                e2.setComment(e.getComment());
                e2.setExtra(e.getExtra());
                if (e.getMethod() == ZipEntry.STORED) {
                    e2.setSize(e.getSize());
                    e2.setCrc(e.getCrc());
                }
                zos.putNextEntry(e2);
                FileUtil.copy(zis, zos);
            } else {
                // replace with the new files
                final File file = relpathToFile.get(name);
                //addFile(file, name, zos);
                relpathToFile.remove(name);
                addFileToZip(zos, file, name, null, null);
            }
        }
        // add the remaining new files
        for (final String path : relpathToFile.keySet()) {
            File file = relpathToFile.get(path);
            addFileToZip(zos, file, path, null, null);
        }
    } finally {
        zis.close();
        zos.close();
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) ZipFile(java.util.zip.ZipFile)

Example 48 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project intellij-community by JetBrains.

the class PrepareToDeployAction method processLibrariesAndJpsPlugins.

private static void processLibrariesAndJpsPlugins(final File jarFile, final File zipFile, final String pluginName, final Set<Library> libs, Map<Module, String> jpsModules, final ProgressIndicator progressIndicator) throws IOException {
    if (FileUtil.ensureCanCreateFile(zipFile)) {
        ZipOutputStream zos = null;
        try {
            zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
            addStructure(pluginName, zos);
            addStructure(pluginName + "/" + MIDDLE_LIB_DIR, zos);
            final String entryName = pluginName + JAR_EXTENSION;
            ZipUtil.addFileToZip(zos, jarFile, getZipPath(pluginName, entryName), new HashSet<>(), createFilter(progressIndicator, FileTypeManager.getInstance()));
            for (Map.Entry<Module, String> entry : jpsModules.entrySet()) {
                File jpsPluginJar = jarModulesOutput(Collections.singleton(entry.getKey()), null, null);
                ZipUtil.addFileToZip(zos, jpsPluginJar, getZipPath(pluginName, entry.getValue()), null, null);
            }
            Set<String> usedJarNames = new HashSet<>();
            usedJarNames.add(entryName);
            Set<VirtualFile> jarredVirtualFiles = new HashSet<>();
            for (Library library : libs) {
                final VirtualFile[] files = library.getFiles(OrderRootType.CLASSES);
                for (VirtualFile virtualFile : files) {
                    if (jarredVirtualFiles.add(virtualFile)) {
                        if (virtualFile.getFileSystem() instanceof JarFileSystem) {
                            addLibraryJar(virtualFile, zipFile, pluginName, zos, usedJarNames, progressIndicator);
                        } else {
                            makeAndAddLibraryJar(virtualFile, zipFile, pluginName, zos, usedJarNames, progressIndicator, library.getName());
                        }
                    }
                }
            }
        } finally {
            if (zos != null)
                zos.close();
        }
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module) XmlFile(com.intellij.psi.xml.XmlFile)

Example 49 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project intellij-community by JetBrains.

the class PrepareToDeployAction method makeAndAddLibraryJar.

private static void makeAndAddLibraryJar(final VirtualFile virtualFile, final File zipFile, final String pluginName, final ZipOutputStream zos, final Set<String> usedJarNames, final ProgressIndicator progressIndicator, final String preferredName) throws IOException {
    File libraryJar = FileUtil.createTempFile(TEMP_PREFIX, JAR_EXTENSION);
    libraryJar.deleteOnExit();
    ZipOutputStream jar = null;
    try {
        jar = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(libraryJar)));
        ZipUtil.addFileOrDirRecursively(jar, libraryJar, VfsUtilCore.virtualToIoFile(virtualFile), "", createFilter(progressIndicator, FileTypeManager.getInstance()), null);
    } finally {
        if (jar != null)
            jar.close();
    }
    final String jarName = getLibraryJarName(virtualFile.getName() + JAR_EXTENSION, usedJarNames, preferredName == null ? null : preferredName + JAR_EXTENSION);
    ZipUtil.addFileOrDirRecursively(zos, zipFile, libraryJar, getZipPath(pluginName, jarName), createFilter(progressIndicator, null), null);
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) JarOutputStream(java.util.jar.JarOutputStream) XmlFile(com.intellij.psi.xml.XmlFile)

Example 50 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project intellij-community by JetBrains.

the class IoTestUtil method createTestJar.

@NotNull
public static File createTestJar(@NotNull File jarFile, @NotNull File root) {
    try (ZipOutputStream stream = new ZipOutputStream(new FileOutputStream(jarFile))) {
        FileUtil.visitFiles(root, file -> {
            if (file.isFile()) {
                String path = FileUtil.toSystemIndependentName(ObjectUtils.assertNotNull(FileUtil.getRelativePath(root, file)));
                try {
                    stream.putNextEntry(new ZipEntry(path));
                    try (InputStream is = new FileInputStream(file)) {
                        FileUtil.copy(is, stream);
                    }
                    stream.closeEntry();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return true;
        });
        return jarFile;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ZipOutputStream (java.util.zip.ZipOutputStream)1168 ZipEntry (java.util.zip.ZipEntry)745 FileOutputStream (java.io.FileOutputStream)561 File (java.io.File)486 IOException (java.io.IOException)393 FileInputStream (java.io.FileInputStream)193 ByteArrayOutputStream (java.io.ByteArrayOutputStream)186 BufferedOutputStream (java.io.BufferedOutputStream)177 ZipFile (java.util.zip.ZipFile)163 InputStream (java.io.InputStream)144 Test (org.junit.Test)128 OutputStream (java.io.OutputStream)109 ByteArrayInputStream (java.io.ByteArrayInputStream)94 ZipInputStream (java.util.zip.ZipInputStream)88 Path (java.nio.file.Path)82 BufferedInputStream (java.io.BufferedInputStream)61 ArrayList (java.util.ArrayList)55 FileNotFoundException (java.io.FileNotFoundException)51 Date (java.util.Date)44 HashMap (java.util.HashMap)44