@Override public int hashCode() { return this.getFile().hashCode(); }
public boolean exists() { return getFile().exists(); }
public void delete() { try { Files.delete(getFile().toPath()); } catch (final IOException e) { throw new CoreException("Cannot delete file", e); } }
/** * @return An list of files in the directory and sub directories if this object a directory. The * list will be empty if the directory is empty. If this object is a file, the list will * contain only the file itself. */ public List<File> listFilesRecursively() { final List<File> result = new ArrayList<>(); if (this.getFile().isDirectory()) { for (final java.io.File file : this.getFile().listFiles()) { final File newFile = new File(file.getAbsolutePath()); if (file.isDirectory()) { result.addAll(newFile.listFilesRecursively()); } else { result.add(newFile); } } } else if (this.getFile().exists()) { result.add(this); } return result; }
@Override protected int finish(final CommandMap command) { final File file = (File) command.get(OUTPUT_PARAMETER); try (PrintStream out = new PrintStream(new FileOutputStream(file.getFile(), true))) { this.turnRestrictions.forEach(value -> out.println(value)); } catch (final IOException oops) { throw new CoreException("Error writing turnRestriction ids to file", oops); } return 0; }
public void deleteRecursively() { final Path folder = getFile().toPath(); try { Files.walkFileTree(folder, new SimpleFileVisitor<Path>() { @Override public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } }); } catch (final IOException e) { throw new CoreException("Cannot delete folder recursively", e); } }
@Override protected int finish(final CommandMap command) { @SuppressWarnings("unchecked") final Optional<File> possibleFile = (Optional<File>) command.getOption(OUTPUT_PARAMETER); if (possibleFile.isPresent()) { try (PrintStream out = new PrintStream( new FileOutputStream(possibleFile.get().getFile(), true))) { this.restrictedPaths.forEach(value -> out.println(value)); } catch (final IOException oops) { throw new CoreException("Error writing restrictedPaths to file", oops); } } else { try (PrintStream out = new PrintStream(System.out)) { this.restrictedPaths.forEach(value -> out.println(value)); } } return 0; }
@Override protected int finish(final CommandMap command) { final File file = (File) command.get(OUTPUT_PARAMETER); try (PrintStream out = new PrintStream( new BufferedOutputStream(new FileOutputStream(file.getFile(), true)))) { for (final String country : this.featureCounts.rowKeySet()) { for (final AtlasType type : AtlasType.values()) { out.println(String.format("%s-%s: %d", country, type, this.featureCounts.contains(country, type) ? this.featureCounts.get(country, type) : 0)); } out.println(); } } catch (final IOException oops) { throw new CoreException("Error writing to file: {}", file.getAbsolutePath().toString(), oops); } return 0; }
this.outputDelegate.printlnStdout(SAVED_TO + outputFile.getFile().getAbsolutePath());
/** * Get or create a {@link CountryBoundaryMap}. If the country-boundary-map parameter is set, * this will attempt to load the text or shape file from that parameter. Else, this will load * using the entire world as the country UNK (unknown). * * @param map * {@link CommandMap} containing the {@code COUNTRY_MAP_PARAMETER} * @return {@link CountryBoundaryMap} loaded from a file or default */ private CountryBoundaryMap getCountryBoundaryMap(final CommandMap map) { final Optional<File> countryMapOption = (Optional<File>) map .getOption(COUNTRY_MAP_PARAMETER); CountryBoundaryMap countryMap = CountryBoundaryMap .fromBoundaryMap(Collections.singletonMap("UNK", MultiPolygon.MAXIMUM)); if (countryMapOption.isPresent()) { final File countryMapFile = countryMapOption.get(); if (FilenameUtils.isExtension(countryMapFile.getName(), "txt")) { countryMap = CountryBoundaryMap.fromPlainText(countryMapFile); } else if (FilenameUtils.isExtension(countryMapFile.getName(), "shp")) { countryMap = CountryBoundaryMap.fromShapeFile(countryMapFile.getFile()); } } return countryMap; }
try (ZipFile file = new ZipFile(getFileSource().getFile())
@Override public Iterable<Resource> entries() { try (ZipFile file = new ZipFile(getFileSource().getFile()) { @Override public void close() { // Do nothing to close the file here, to avoid cutting the legs off the just created // ZipEntry-based resource. } }) { return Iterables.translate(Iterables.from(file.entries()), entry -> { return new InputStreamResource(inputStreamSupplier(file, entry)) .withName(entry.getName()); }); } catch (final IOException e) { throw new CoreException("Cannot get entries from the Zipfile {}.", this.getFileSource().getName(), e); } }
map.readFromShapeFile(shapeFile.getFile());
@Test public void testHighCompressionLevel() throws IOException { final File source = File.temporary(); logger.info("testCompressionLevel using {}", source); try { final ZipFileWritableResource zipFile = new ZipFileWritableResource(source); zipFile.writeAndClose( new StringResource("HereIsSomeTextThatRepeatsHereIsSomeTextThatRepeats") .withName(NAME_1), new StringResource("HereIsSomeTextThatDoesn'tRepeat").withName(NAME_2)); final ZipFile file = new ZipFile(source.getFile()); final ZipEntry name1 = file.getEntry(NAME_1); Assert.assertNotEquals(-1, name1.getCompressedSize()); Assert.assertTrue(name1.getCompressedSize() < name1.getSize()); final ZipEntry name2 = file.getEntry(NAME_2); Assert.assertNotEquals(-1, name2.getCompressedSize()); Assert.assertTrue(name2.getCompressedSize() >= name2.getSize()); file.close(); } finally { source.delete(); logger.info("testZipFile deleted {}", source); } }
@Test public void testNoCompressionLevel() throws IOException { final File source = File.temporary(); logger.info("testCompressionLevel using {}", source); try { final ZipFileWritableResource zipFile = new ZipFileWritableResource(source); zipFile.setWriteCompression(false); zipFile.writeAndClose( new StringResource("HereIsSomeTextThatRepeatsHereIsSomeTextThatRepeats") .withName(NAME_1), new StringResource("HereIsSomeTextThatDoesn'tRepeat").withName(NAME_2)); final ZipFile file = new ZipFile(source.getFile()); final ZipEntry name1 = file.getEntry(NAME_1); Assert.assertNotEquals(-1, name1.getCompressedSize()); Assert.assertTrue(name1.getCompressedSize() >= name1.getSize()); final ZipEntry name2 = file.getEntry(NAME_2); Assert.assertNotEquals(-1, name2.getCompressedSize()); Assert.assertTrue(name2.getCompressedSize() >= name2.getSize()); file.close(); } finally { source.delete(); logger.info("testZipFile deleted {}", source); } }