FileSystems.rename(srcFiles, dstFiles, StandardMoveOptions.IGNORE_MISSING_FILES); removeTemporaryFiles(srcFiles);
FileSystems.rename( ImmutableList.of(tempFile), ImmutableList.of(outputFile),
@ProcessElement public void processElement(ProcessContext context) { ResourceId inputFile = context.element().resourceId(); Compression compression = compressionValue.get(); // Add the compression extension to the output filename. Example: demo.txt -> demo.txt.gz String outputFilename = inputFile.getFilename() + compression.getSuggestedSuffix(); // Resolve the necessary resources to perform the transfer ResourceId outputDir = FileSystems.matchNewResource(destinationLocation.get(), true); ResourceId outputFile = outputDir.resolve(outputFilename, StandardResolveOptions.RESOLVE_FILE); ResourceId tempFile = outputDir.resolve("temp-" + outputFilename, StandardResolveOptions.RESOLVE_FILE); // Perform the copy of the compressed channel to the destination. try (ReadableByteChannel readerChannel = FileSystems.open(inputFile)) { try (WritableByteChannel writerChannel = compression.writeCompressed(FileSystems.create(tempFile, MimeTypes.BINARY))) { // Execute the copy to the temporary file ByteStreams.copy(readerChannel, writerChannel); } // Rename the temporary file to the output file FileSystems.rename(ImmutableList.of(tempFile), ImmutableList.of(outputFile)); // Output the path to the uncompressed file context.output(outputFile.toString()); } catch (IOException e) { LOG.error("Error occurred during compression of {}", inputFile.toString(), e); context.output(DEADLETTER_TAG, KV.of(inputFile.toString(), e.getMessage())); } } }
@Test public void testRenameIgnoreMissingFiles() throws Exception { Path srcPath1 = temporaryFolder.newFile().toPath(); Path nonExistentPath = srcPath1.resolveSibling("non-existent"); Path srcPath3 = temporaryFolder.newFile().toPath(); Path destPath1 = srcPath1.resolveSibling("dest1"); Path destPath2 = nonExistentPath.resolveSibling("dest2"); Path destPath3 = srcPath1.resolveSibling("dest3"); createFileWithContent(srcPath1, "content1"); createFileWithContent(srcPath3, "content3"); FileSystems.rename( toResourceIds( ImmutableList.of(srcPath1, nonExistentPath, srcPath3), false /* isDirectory */), toResourceIds(ImmutableList.of(destPath1, destPath2, destPath3), false /* isDirectory */), MoveOptions.StandardMoveOptions.IGNORE_MISSING_FILES); assertFalse(srcPath1.toFile().exists()); assertFalse(srcPath3.toFile().exists()); assertThat( Files.readLines(destPath1.toFile(), StandardCharsets.UTF_8), containsInAnyOrder("content1")); assertFalse(destPath2.toFile().exists()); assertThat( Files.readLines(destPath3.toFile(), StandardCharsets.UTF_8), containsInAnyOrder("content3")); }
@Test public void testRenameThrowsNoSuchFileException() throws Exception { Path existingPath = temporaryFolder.newFile().toPath(); Path nonExistentPath = existingPath.resolveSibling("non-existent"); Path destPath1 = existingPath.resolveSibling("dest1"); Path destPath2 = nonExistentPath.resolveSibling("dest2"); createFileWithContent(existingPath, "content1"); thrown.expect(NoSuchFileException.class); FileSystems.rename( toResourceIds(ImmutableList.of(existingPath, nonExistentPath), false /* isDirectory */), toResourceIds(ImmutableList.of(destPath1, destPath2), false /* isDirectory */)); }