Search in sources :

Example 1 with NotClosableInputStream

use of org.eclipse.che.api.vfs.util.NotClosableInputStream 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 2 with NotClosableInputStream

use of org.eclipse.che.api.vfs.util.NotClosableInputStream in project che by eclipse.

the class TarArchiver method extract.

@Override
public void extract(InputStream tarInput, boolean overwrite, int stripNumber) throws IOException, ForbiddenException, ConflictException, ServerException {
    try (TarArchiveInputStream tarInputStream = new TarArchiveInputStream(tarInput)) {
        InputStream notClosableInputStream = new NotClosableInputStream(tarInputStream);
        TarArchiveEntry tarEntry;
        while ((tarEntry = tarInputStream.getNextTarEntry()) != null) {
            VirtualFile extractFolder = folder;
            Path relativePath = Path.of(tarEntry.getName());
            if (stripNumber > 0) {
                if (relativePath.length() <= stripNumber) {
                    continue;
                }
                relativePath = relativePath.subPath(stripNumber);
            }
            if (tarEntry.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()));
                }
            }
        }
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ConflictException(org.eclipse.che.api.core.ConflictException) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) NotClosableInputStream(org.eclipse.che.api.vfs.util.NotClosableInputStream) InputStream(java.io.InputStream) NotClosableInputStream(org.eclipse.che.api.vfs.util.NotClosableInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Aggregations

InputStream (java.io.InputStream)2 ConflictException (org.eclipse.che.api.core.ConflictException)2 NotClosableInputStream (org.eclipse.che.api.vfs.util.NotClosableInputStream)2 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)1 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)1