Search in sources :

Example 26 with ZipEntry

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

the class ZipFileStructureProvider method initialize.

/**
     * Initializes this object's children table based on the contents of
     * the specified source file.
     */
protected void initialize() {
    children = new HashMap(1000);
    Enumeration entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) entries.nextElement();
        if (!entry.isDirectory()) {
            IPath path = new Path(entry.getName()).addTrailingSeparator();
            int pathSegmentCount = path.segmentCount();
            for (int i = 1; i < pathSegmentCount; i++) {
                createContainer(path.uptoSegment(i));
            }
            createFile(entry);
        }
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) Enumeration(java.util.Enumeration) IPath(org.eclipse.core.runtime.IPath) HashMap(java.util.HashMap) ZipEntry(java.util.zip.ZipEntry)

Example 27 with ZipEntry

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

the class InstallExtension method getExtensionFromFile.

private static Extension getExtensionFromFile(Path zipPath) throws IOException {
    try (ZipFile zipFile = new ZipFile(zipPath.toString())) {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        ZipEntry gwtXmlEntry = null;
        ZipEntry pomEntry = null;
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (!entry.isDirectory()) {
                if (entry.getName().endsWith(GwtXmlUtils.GWT_MODULE_XML_SUFFIX)) {
                    gwtXmlEntry = entry;
                } else if (entry.getName().endsWith("pom.xml")) {
                    pomEntry = entry;
                }
            }
            // have both entries
            if (pomEntry != null && gwtXmlEntry != null) {
                break;
            }
        }
        // TODO consider Codenvy extension validator
        if (pomEntry == null) {
            return null;
        }
        String gwtModuleName = null;
        if (gwtXmlEntry != null) {
            gwtModuleName = gwtXmlEntry.getName().replace(File.separatorChar, '.');
            gwtModuleName = gwtModuleName.substring(0, gwtModuleName.length() - GwtXmlUtils.GWT_MODULE_XML_SUFFIX.length());
        }
        Model pom = Model.readFrom(zipFile.getInputStream(pomEntry));
        return new Extension(gwtModuleName, MavenUtils.getGroupId(pom), pom.getArtifactId(), MavenUtils.getVersion(pom));
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) Model(org.eclipse.che.ide.maven.tools.Model)

Example 28 with ZipEntry

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

the class ZipArchiverTest method assertThatZipArchiveContainsAllEntries.

private void assertThatZipArchiveContainsAllEntries(InputStream in, Map<String, String> entries) throws Exception {
    try (ZipInputStream zip = new ZipInputStream(in)) {
        ZipEntry zipEntry;
        while ((zipEntry = zip.getNextEntry()) != null) {
            String name = zipEntry.getName();
            assertTrue(String.format("Unexpected entry %s in zip", name), entries.containsKey(name));
            if (!zipEntry.isDirectory()) {
                String content = new String(ByteStreams.toByteArray(zip));
                assertEquals(String.format("Invalid content of file %s", name), entries.get(name), content);
            }
            entries.remove(name);
            zip.closeEntry();
        }
    }
    assertTrue(String.format("Expected but were not found in zip %s", entries), entries.isEmpty());
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry)

Example 29 with ZipEntry

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

the class ZipArchiver method extract.

@Override
public void extract(InputStream zipInput, boolean overwrite, int stripNumber) throws IOException, ForbiddenException, ConflictException, ServerException {
    try (ZipInputStream zip = new ZipInputStream(ZipContent.of(zipInput).getContent())) {
        InputStream notClosableInputStream = new NotClosableInputStream(zip);
        ZipEntry zipEntry;
        while ((zipEntry = zip.getNextEntry()) != null) {
            VirtualFile extractFolder = folder;
            Path relativePath = Path.of(zipEntry.getName());
            if (stripNumber > 0) {
                if (relativePath.length() <= stripNumber) {
                    continue;
                }
                relativePath = relativePath.subPath(stripNumber);
            }
            if (zipEntry.isDirectory()) {
                if (!extractFolder.hasChild(relativePath)) {
                    extractFolder.createFolder(relativePath.toString());
                }
                continue;
            }
            if (relativePath.length() > 1) {
                Path neededParentPath = relativePath.getParent();
                VirtualFile neededParent = extractFolder.getChild(neededParentPath);
                if (neededParent == null) {
                    neededParent = extractFolder.createFolder(neededParentPath.toString());
                }
                extractFolder = neededParent;
            }
            String fileName = relativePath.getName();
            VirtualFile file = extractFolder.getChild(Path.of(fileName));
            if (file == null) {
                extractFolder.createFile(fileName, notClosableInputStream);
            } else {
                if (overwrite) {
                    file.updateContent(notClosableInputStream);
                } else {
                    throw new ConflictException(String.format("File '%s' already exists", file.getPath()));
                }
            }
            zip.closeEntry();
        }
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ConflictException(org.eclipse.che.api.core.ConflictException) ZipInputStream(java.util.zip.ZipInputStream) NotClosableInputStream(org.eclipse.che.api.vfs.util.NotClosableInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) NotClosableInputStream(org.eclipse.che.api.vfs.util.NotClosableInputStream)

Example 30 with ZipEntry

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

the class ProjectManagerWriteTest method testImportProject.

@Test
public void testImportProject() throws Exception {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    String fileContent = "to be or not to be";
    ZipOutputStream zipOut = new ZipOutputStream(bout);
    zipOut.putNextEntry(new ZipEntry("folder1/"));
    zipOut.putNextEntry(new ZipEntry("folder1/file1.txt"));
    zipOut.putNextEntry(new ZipEntry("file1"));
    zipOut.write(fileContent.getBytes());
    zipOut.close();
    final InputStream zip = new ByteArrayInputStream(bout.toByteArray());
    final String importType = "_123_";
    registerImporter(importType, zip);
    SourceStorage sourceConfig = DtoFactory.newDto(SourceStorageDto.class).withType(importType);
    pm.importProject("/testImportProject", sourceConfig, false, () -> new ProjectImportOutputWSLineConsumer("BATCH", "ws", 300));
    RegisteredProject project = projectRegistry.getProject("/testImportProject");
    assertNotNull(project);
    // BASE
    //System.out.println(">>> "+project.getProjectType());
    assertNotNull(project.getBaseFolder().getChild("file1"));
    assertEquals(fileContent, project.getBaseFolder().getChild("file1").getVirtualFile().getContentAsString());
}
Also used : SourceStorage(org.eclipse.che.api.core.model.project.SourceStorage) SourceStorageDto(org.eclipse.che.api.workspace.shared.dto.SourceStorageDto) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ProjectImportOutputWSLineConsumer(org.eclipse.che.api.project.server.importer.ProjectImportOutputWSLineConsumer) Test(org.junit.Test)

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