@Override public void copyDirectory(String from, String to) { InputStream response = executor.getFileOrDirectoryFromContainerAsTar(cubeId, from); Path toPath = Paths.get(to); File toPathFile = toPath.toFile(); if (toPathFile.exists() && toPathFile.isFile()) { throw new IllegalArgumentException(String.format( "%s parameter should be a directory in copy operation but you set an already existing file not a directory. Check %s in your local directory because currently is a file.", "to", toPath.normalize().toString())); } try { Files.createDirectories(toPath); IOUtil.untar(response, toPathFile); } catch (IOException e) { throw new IllegalStateException(e); } }
@Override public void copyDirectory(String from, String to) { InputStream response = executor.getFileOrDirectoryFromContainerAsTar(cubeId, from); Path toPath = Paths.get(to); File toPathFile = toPath.toFile(); if (toPathFile.exists() && toPathFile.isFile()) { throw new IllegalArgumentException(String.format( "%s parameter should be a directory in copy operation but you set an already existing file not a directory. Check %s in your local directory because currently is a file.", "to", toPath.normalize().toString())); } try { Files.createDirectories(toPath); IOUtil.untar(response, toPathFile); } catch (IOException e) { throw new IllegalStateException(e); } }
private void executeCopyAction(DockerClientExecutor dockerClientExecutor, String containerId, Copy configurationParameters) throws IOException { String to = null; String from = null; if (configurationParameters.getTo() != null && configurationParameters.getFrom() != null) { to = configurationParameters.getTo(); from = configurationParameters.getFrom(); } else { throw new IllegalArgumentException( String.format("to and from property is mandatory when copying files from container %s.", containerId)); } InputStream response = dockerClientExecutor.getFileOrDirectoryFromContainerAsTar(containerId, from); Path toPath = Paths.get(to); File toPathFile = toPath.toFile(); if (toPathFile.exists() && toPathFile.isFile()) { throw new IllegalArgumentException(String.format( "%s parameter should be a directory in copy operation but you set an already existing file not a directory. Check %s in your local directory because currently is a file.", "to", toPath.normalize().toString())); } Files.createDirectories(toPath); IOUtil.untar(response, toPathFile); } }
private void executeCopyAction(DockerClientExecutor dockerClientExecutor, String containerId, Copy configurationParameters) throws IOException { String to = null; String from = null; if (configurationParameters.getTo() != null && configurationParameters.getFrom() != null) { to = configurationParameters.getTo(); from = configurationParameters.getFrom(); } else { throw new IllegalArgumentException( String.format("to and from property is mandatory when copying files from container %s.", containerId)); } InputStream response = dockerClientExecutor.getFileOrDirectoryFromContainerAsTar(containerId, from); Path toPath = Paths.get(to); File toPathFile = toPath.toFile(); if (toPathFile.exists() && toPathFile.isFile()) { throw new IllegalArgumentException(String.format( "%s parameter should be a directory in copy operation but you set an already existing file not a directory. Check %s in your local directory because currently is a file.", "to", toPath.normalize().toString())); } Files.createDirectories(toPath); IOUtil.untar(response, toPathFile); } }
@Override public void copyDirectory(String from, String to) { Path toPath = Paths.get(to); File toPathFile = toPath.toFile(); if(toPathFile.exists() && toPathFile.isFile()) { throw new IllegalArgumentException(String.format("%s parameter should be a directory in copy operation but you set an already existing file not a directory. Check %s in your local directory because currently is a file.", "to", toPath.normalize().toString())); } try { Files.createDirectories(toPath); final String fileOrDir; if (from.endsWith("/")) { fileOrDir = "."; } else { Path fromPath = FileSystems.getDefault().getPath(from); fileOrDir = fromPath.getFileName().toString(); from = fromPath.getParent().toString(); } try (ExecWatch watch = client.getClient().inNamespace(client.getClient().getNamespace()).pods().withName(cubeId).exec("tar", "-C", from, "-c", fileOrDir)) { IOUtil.untar(watch.getOutput(), toPathFile); } } catch (IOException e) { throw new IllegalStateException(e); } }
@Test public void shouldTarFile() throws IOException { final File file = temporaryFolder.newFile("content.txt"); final FileOutputStream output = new FileOutputStream(file); IOUtils.copy(new ByteArrayInputStream("hello".getBytes()), output); output.flush(); output.close(); File outputFolder = temporaryFolder.newFolder(); IOUtil.tar(file, new File(outputFolder, "x.tar")); IOUtil.untar(new FileInputStream(new File(outputFolder, "x.tar")), outputFolder); final String content = IOUtil.asString(new FileInputStream(new File(outputFolder, "content.txt"))); assertThat(content, is("hello")); }