public Element exec(Element params, ServiceContext context) throws Exception {

    Path harvestingLogoDirectory = Resources.locateHarvesterLogosDir(context);
    Path nodeLogoDirectory = Resources.locateLogosDir(context);

    String file = Util.getParam(params, Params.FNAME);
    String asFavicon = Util.getParam(params, Params.FAVICON, "0");

    if (file.contains("..")) {
      throw new BadParameterEx("Invalid character found in resource name.", file);
    }

    if ("".equals(file)) {
      throw new Exception("Logo name is not defined.");
    }

    SettingManager settingMan = context.getBean(SettingManager.class);
    String nodeUuid = settingMan.getSiteId();

    try {
      Path logoFilePath = harvestingLogoDirectory.resolve(file);
      if (!Files.exists(logoFilePath)) {
        logoFilePath = context.getAppPath().resolve("images/harvesting/" + file);
      }
      try (InputStream inputStream = Files.newInputStream(logoFilePath)) {
        BufferedImage source = ImageIO.read(inputStream);

        if ("1".equals(asFavicon)) {
          createFavicon(source, nodeLogoDirectory.resolve("favicon.png"));
        } else {
          Path logo = nodeLogoDirectory.resolve(nodeUuid + ".png");
          Path defaultLogo = nodeLogoDirectory.resolve("logo.png");

          if (!file.endsWith(".png")) {
            try (OutputStream logoOut = Files.newOutputStream(logo);
                OutputStream defLogoOut = Files.newOutputStream(defaultLogo); ) {
              ImageIO.write(source, "png", logoOut);
              ImageIO.write(source, "png", defLogoOut);
            }
          } else {
            Files.deleteIfExists(logo);
            IO.copyDirectoryOrFile(logoFilePath, logo, false);
            Files.deleteIfExists(defaultLogo);
            IO.copyDirectoryOrFile(logoFilePath, defaultLogo, false);
          }
        }
      }
    } catch (Exception e) {
      throw new Exception(
          "Unable to move uploaded thumbnail to destination directory. Error: " + e.getMessage());
    }

    Element response = new Element("response");
    response.addContent(new Element("status").setText("Logo set."));
    return response;
  }
  @Override
  public void createNewPropertyFile() {
    Properties props = new Properties();
    // The path of the properties file
    Path txtFile = Paths.get(Configurations.CONFIG_PROPERTIES_FILE_PATH);
    System.out.println("creating config file at " + txtFile.toString());
    // Opening input and output streams for writing and reading files
    try (OutputStream txtOutStream = Files.newOutputStream(txtFile); ) {
      // Initialize the properties with 2 values

      props.setProperty("server.port", "3000");
      props.setProperty("keepbackup", String.valueOf(true));
      props.setProperty("path.source", Configurations.USER_DIRECTORY);
      props.setProperty("path.output", Configurations.USER_DIRECTORY + "output" + File.separator);
      props.setProperty("path.backup", Configurations.USER_DIRECTORY + "backup" + File.separator);
      props.setProperty("folder.name.assets", "assets");
      props.setProperty("folder.name.content", "content");
      props.setProperty("folder.name.templates", "templates");
      props.setProperty("output.minify", String.valueOf(true));

      // writing properties into properties file from Java
      props.store(txtOutStream, "Text Properties file generated from Shammy");
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
Exemple #3
0
  /**
   * Sends a GET request and returns the response.
   *
   * @param endpoint The endpoint to send the request to.
   * @param headers Any additional headers to send with this request. You can use {@link
   *     org.apache.http.HttpHeaders} constants for header names.
   * @return A {@link Path} to the downloaded content, if any.
   * @throws IOException If an error occurs.
   * @see java.nio.file.Files#probeContentType(Path)
   */
  public Response<Path> get(Endpoint endpoint, NameValuePair... headers) throws IOException {

    // Create the request
    HttpGet get = new HttpGet(endpoint.url());
    get.setHeaders(combineHeaders(headers));
    Path tempFile = null;

    // Send the request and process the response
    try (CloseableHttpResponse response = httpClient().execute(get)) {

      if (response.getStatusLine().getStatusCode() != HttpStatus.OK_200) {
        return null;
      } // If bad response return null

      // Request the content
      HttpEntity entity = response.getEntity();

      // Download the content to a temporary file
      if (entity != null) {
        tempFile = Files.createTempFile("download", "file");
        try (InputStream input = entity.getContent();
            OutputStream output = Files.newOutputStream(tempFile)) {
          IOUtils.copy(input, output);
        }
      }

      return new Response<>(response.getStatusLine(), tempFile);
    }
  }
 // Blindly append entries to the end of the log. Note that there is
 // no check to make sure that the last entry is from the correct
 // term. This method should only be used in testing.
 //
 // @param entries to append (in order of 0 to append.length-1)
 // @return highest index in log after entries have been appended.
 public int append(Entry[] entries) {
   try {
     if (entries != null) {
       OutputStream out =
           Files.newOutputStream(
               mLogPath,
               StandardOpenOption.CREATE,
               StandardOpenOption.APPEND,
               StandardOpenOption.SYNC);
       for (Entry entry : entries) {
         if (entry != null) {
           out.write(entry.toString().getBytes());
           out.write('\n');
           mEntries.add(entry);
         } else {
           //	    System.out.println ("Tried to append null entry to RaftLog.");
           break;
         }
       }
       out.close();
     }
   } catch (IOException e) {
     System.out.println(e.getMessage());
     e.printStackTrace();
   }
   return (mEntries.size() - 1);
 }
  void doOCROnCurrentPage() throws IOException, TikaException, SAXException {
    if (config.getOCRStrategy().equals(NO_OCR)) {
      return;
    }
    TesseractOCRConfig tesseractConfig =
        context.get(TesseractOCRConfig.class, DEFAULT_TESSERACT_CONFIG);

    TesseractOCRParser tesseractOCRParser = new TesseractOCRParser();
    if (!tesseractOCRParser.hasTesseract(tesseractConfig)) {
      throw new TikaException(
          "Tesseract is not available. "
              + "Please set the OCR_STRATEGY to NO_OCR or configure Tesseract correctly");
    }

    PDFRenderer renderer = new PDFRenderer(pdDocument);
    TemporaryResources tmp = new TemporaryResources();
    try {
      BufferedImage image = renderer.renderImage(pageIndex, 2.0f, config.getOCRImageType());
      Path tmpFile = tmp.createTempFile();
      try (OutputStream os = Files.newOutputStream(tmpFile)) {
        // TODO: get output format from TesseractConfig
        ImageIOUtil.writeImage(image, config.getOCRImageFormatName(), os, config.getOCRDPI());
      }
      try (InputStream is = TikaInputStream.get(tmpFile)) {
        tesseractOCRParser.parseInline(is, xhtml, tesseractConfig);
      }
    } catch (IOException e) {
      handleCatchableIOE(e);
    } catch (SAXException e) {
      throw new IOExceptionWithCause("error writing OCR content from PDF", e);
    } finally {
      tmp.dispose();
    }
  }
  /**
   * Marshals the object to the given path that must represent a path to the file.
   *
   * <p>This method is capable of setting the schema version to the object being marshalled.
   *
   * @param path Path to file
   * @param object Object to marshal
   * @param noNamespaceSchemaLocation NoNamespaceSchemaLocation to set. If it's <code>null</code> no
   *     location will be set.
   * @param schemaVersion If schema version is set and object to marshall is instance of {@link
   *     ISchemaVersionAware} then the given schema version will be set to the object. Use <code>0
   *     </code> to ignore setting of schema version.
   * @throws JAXBException If {@link JAXBException} occurs.
   * @throws IOException If {@link IOException} occurs.
   */
  public void marshall(
      Path path, Object object, String noNamespaceSchemaLocation, int schemaVersion)
      throws JAXBException, IOException {
    if (Files.isDirectory(path)) {
      throw new IOException("Can not marshal object to the path that represents the directory");
    }
    Files.deleteIfExists(path);
    Files.createDirectories(path.getParent());

    JAXBContext context = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    if (null != noNamespaceSchemaLocation) {
      marshaller.setProperty(
          Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, noNamespaceSchemaLocation);
    }

    // set schema version if needed
    if ((object instanceof ISchemaVersionAware) && (0 != schemaVersion)) {
      ((ISchemaVersionAware) object).setSchemaVersion(schemaVersion);
    }

    try (OutputStream outputStream = Files.newOutputStream(path, StandardOpenOption.CREATE_NEW)) {
      marshaller.marshal(object, outputStream);
    }
  }
  /**
   * Do actual encryption/decryption from source to destination file.
   *
   * @return <code>true</code> if file is successfully encrypted/decrypted, otherwise <code>false
   *     </code>.
   */
  public boolean process() {
    try {
      InputStream inputStream = Files.newInputStream(inputPath, StandardOpenOption.READ);
      OutputStream outputStream = Files.newOutputStream(outputPath, StandardOpenOption.WRITE);

      byte[] inputBuffer = new byte[4096];
      while (true) {
        int byteCount = inputStream.read(inputBuffer);
        if (byteCount < 1) break;

        byte[] outputBuffer = new byte[4096];
        int outputByteCount = cipher.update(inputBuffer, 0, byteCount, outputBuffer);
        outputStream.write(outputBuffer, 0, outputByteCount);
      }

      byte[] outputBuffer = new byte[4096];
      int outputByteCount = cipher.doFinal(outputBuffer, 0);
      outputStream.write(outputBuffer, 0, outputByteCount);
    } catch (ShortBufferException e) {
      return false;
    } catch (IllegalBlockSizeException e) {
      return false;
    } catch (BadPaddingException e) {
      return false;
    } catch (IOException e) {
      return false;
    }

    return true;
  }
  /**
   * 下载指定pdf
   *
   * @param url pdf地址
   * @return 下载后保存的全路径
   * @throws Exception
   */
  public String downloadPdf(String url) throws Exception {
    URL u = new URL(url);
    HttpURLConnection conn = (HttpURLConnection) u.openConnection();
    conn.setRequestMethod("GET");
    conn.connect();
    InputStream is = conn.getInputStream();

    String fileName = getTimestampFromUrl(url);
    Path tmp = Paths.get(SAVED_DIR).resolve(TMP_FILE);
    Path out = Paths.get(SAVED_DIR).resolve(fileName + ".pdf");
    OutputStream os = Files.newOutputStream(tmp);
    log.info(String.format("Downloading %s (%s)", u.toString(), conn.getURL().toString()));

    // 开始下载
    byte[] buffer = new byte[1024 * 1024];
    int len;
    long size = 0;
    while ((len = is.read(buffer)) > -1) {
      os.write(buffer, 0, len);
      size += len;
      log.info(size + " bytes has been downloaded...");
    }

    Files.move(tmp, out, StandardCopyOption.REPLACE_EXISTING);
    log.info("Downloaded " + out.toString() + " finished");
    conn.disconnect();

    return out.toString();
  }
Exemple #9
0
 public OutputStream outputStream() {
   try {
     return Files.newOutputStream(file);
   } catch (IOException e) {
     throw New.unchecked(e);
   }
 }
  public static void saveData(String file, EnumMap<PlayerStats, Float> map, boolean overwrite) {
    FileSystem fileSystem = FileSystems.getDefault();
    Path path = fileSystem.getPath(file);
    if (Files.isDirectory(path))
      throw new IllegalArgumentException("This method is not supposed to read a directory");

    if (Files.exists(path) && Files.isReadable(path)) {
      if (overwrite == false) return;
    } else
      try {
        Files.createFile(path);
      } catch (IOException e) {
        e
            .printStackTrace(); // To change body of catch statement use File | Settings | File
                                // Templates.
      }

    try (ObjectOutputStream objectOutputStream =
        new ObjectOutputStream(Files.newOutputStream(path))) {
      objectOutputStream.writeObject(map);
    } catch (IOException e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    }
  }
Exemple #11
0
 /**
  * @param db the repository
  * @param in an {@link InputStream} providing the original content
  * @param out the {@link OutputStream} into which the content of the pointer file should be
  *     written. That's the content which will be added to the git repository
  * @throws IOException when the creation of the temporary file fails or when no {@link
  *     OutputStream} for this file can be created
  */
 public CleanFilter(Repository db, InputStream in, OutputStream out) throws IOException {
   super(in, out);
   lfsUtil = new Lfs(db.getDirectory().toPath().resolve("lfs")); // $NON-NLS-1$
   Files.createDirectories(lfsUtil.getLfsTmpDir());
   tmpFile = lfsUtil.createTmpFile();
   tmpOut = Files.newOutputStream(tmpFile, StandardOpenOption.CREATE);
   this.dOut = new DigestOutputStream(tmpOut, Constants.newMessageDigest());
 }
 /** creates a fake jar file with empty class files */
 static void writeJar(Path jar, String... classes) throws IOException {
   try (ZipOutputStream stream = new ZipOutputStream(Files.newOutputStream(jar))) {
     for (String clazz : classes) {
       stream.putNextEntry(
           new ZipEntry(clazz + ".class")); // no package names, just support simple classes
     }
   }
 }
 public static void write(Path path, Map<AggregationKey, AggregationValue> map) {
   if (map.size() > 0) {
     try (OutputStream os = Files.newOutputStream(path);
         ObjectOutputStream oos = new ObjectOutputStream(os)) {
       oos.writeObject(map);
     } catch (Exception e) {
       log.error(e);
     }
   }
 }
 @Override
 protected JarOutputStream getJarOutputStream(Path outputWarFilePath, Manifest inputWarManifest)
     throws WorkflowException {
   try {
     return new JarOutputStream(
         Files.newOutputStream(outputWarFilePath, StandardOpenOption.WRITE), inputWarManifest);
   } catch (IOException e) {
     throw new WorkflowException("could not create output .war file: " + e);
   }
 }
 public void testZipRelativeOutsideEntryName() throws Exception {
   Tuple<Path, Environment> env = createEnv(fs, temp);
   Path zip = createTempDir().resolve("broken.zip");
   try (ZipOutputStream stream = new ZipOutputStream(Files.newOutputStream(zip))) {
     stream.putNextEntry(new ZipEntry("elasticsearch/../blah"));
   }
   String pluginZip = zip.toUri().toURL().toString();
   IOException e = expectThrows(IOException.class, () -> installPlugin(pluginZip, env.v1()));
   assertTrue(e.getMessage(), e.getMessage().contains("resolving outside of plugin directory"));
 }
 public void createDatabaseFile(Path path) {
   Charset charset = Charset.forName("US-ASCII");
   Path filepath = Paths.get("resources/system/" + path);
   if (!Files.exists(filepath)) {
     try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(filepath, CREATE))) {
       JOptionPane.showMessageDialog(null, "Database.dat file create Succesfully");
     } catch (IOException ioe) {
       JOptionPane.showMessageDialog(null, ioe + " from createdatabasefile");
     }
   }
 }
Exemple #17
0
 @Override
 public OutputStream getCacheOutputStream(SourceId sourceId) {
   if (sourceId.getStorage() != this) return null;
   Path file = cacheFileForSource(sourceId);
   try {
     Files.createDirectories(file.getParent());
     return Files.newOutputStream(file);
   } catch (IOException ignored) {
   }
   return null;
 }
  @Slow
  @Test
  public void testProxyWithBigResponseContentWithSlowReader() throws Exception {
    prepareProxy();

    // Create a 6 MiB file
    final int length = 6 * 1024;
    Path targetTestsDir = MavenTestingUtils.getTargetTestingDir().toPath();
    Files.createDirectories(targetTestsDir);
    final Path temp = Files.createTempFile(targetTestsDir, "test_", null);
    byte[] kb = new byte[1024];
    new Random().nextBytes(kb);
    try (OutputStream output = Files.newOutputStream(temp, StandardOpenOption.CREATE)) {
      for (int i = 0; i < length; ++i) output.write(kb);
    }

    prepareServer(
        new HttpServlet() {
          @Override
          protected void doGet(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
            try (InputStream input = Files.newInputStream(temp)) {
              IO.copy(input, response.getOutputStream());
            }
          }
        });

    Request request =
        client.newRequest("localhost", serverConnector.getLocalPort()).path("/proxy/test");
    final CountDownLatch latch = new CountDownLatch(1);
    request.send(
        new BufferingResponseListener(2 * length * 1024) {
          @Override
          public void onContent(Response response, ByteBuffer content) {
            try {
              // Slow down the reader
              TimeUnit.MILLISECONDS.sleep(5);
              super.onContent(response, content);
            } catch (InterruptedException x) {
              response.abort(x);
            }
          }

          @Override
          public void onComplete(Result result) {
            Assert.assertFalse(result.isFailed());
            Assert.assertEquals(200, result.getResponse().getStatus());
            Assert.assertEquals(length * 1024, getContent().length);
            latch.countDown();
          }
        });
    Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
  }
  public void writeFile(Path pathToFile) {

    int keysInFile = 0;
    Path currentFile = pathToFile;

    keysInFile = dbMap.size();

    if (!Files.exists(currentFile) && keysInFile == 0) {
      return;
    }

    if (!Files.exists(currentFile) && keysInFile > 0) {
      try {
        Files.createFile(currentFile);
      } catch (IOException ioexc) {
        throw new DatabaseException("couldnt create " + currentFile.toString());
      }
    }

    /*  if (keysInFile == 0){
        try {
            Files.delete(currentFile);
        } catch (IOException ioexc) {
            throw new DatabaseException("cant delete empty file " + currentFile.toString());
        }
        return;
    }*/

    try {
      DataOutputStream fileToWrite = new DataOutputStream(Files.newOutputStream(currentFile));

      for (Map.Entry<String, String> entry : dbMap.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        byte[] byteKey = key.getBytes("UTF-8");
        byte[] byteValue = value.getBytes("UTF-8");
        fileToWrite.writeInt(byteKey.length);
        fileToWrite.write(byteKey);
        fileToWrite.writeInt(byteValue.length);
        fileToWrite.write(byteValue);
      }
      try {
        fileToWrite.close();
      } catch (IOException ioexc) {
        throw new DatabaseException("can't close DataOutputStream for" + currentFile.toString());
      }
    } catch (IOException iexc) {
      throw new DatabaseException("something wrong with writing to file");
    }
  }
Exemple #20
0
  @Override
  public OutputStream getOutputStream(String first, String... more) throws IOException {
    Path relPath = fileSystem.getPath(first, more);
    Path absPath = root.resolve(relPath);

    log.info("Writing to " + zipFile + absPath.toString());
    Files.createDirectories(absPath.getParent());

    // opens the file for writing, creating the file if it doesn't exist,
    // or initially truncating an existing regular-file to a size of 0 if it exists.
    OutputStream stream = Files.newOutputStream(absPath, CREATE, TRUNCATE_EXISTING, WRITE);

    return stream;
  }
Exemple #21
0
  public void save(Path file, Text text) {
    qprint.verbose("Save pressed");
    String s = text.getText();
    byte data[] = s.getBytes();

    OutputStream out;
    try {
      out = new BufferedOutputStream(Files.newOutputStream(file, CREATE, WRITE));
      qprint.verbose("message to write: " + s);
      out.write(data, 0, data.length);
      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  private void save(ComputationGraph net, String confOut, String paramOut, String updaterOut)
      throws IOException {
    String confJSON = net.getConfiguration().toJson();
    INDArray params = net.params();
    ComputationGraphUpdater updater = net.getUpdater();

    FileUtils.writeStringToFile(new File(confOut), confJSON, encoding);
    try (DataOutputStream dos = new DataOutputStream(Files.newOutputStream(Paths.get(paramOut)))) {
      Nd4j.write(params, dos);
    }

    try (ObjectOutputStream oos =
        new ObjectOutputStream(new FileOutputStream(new File(updaterOut)))) {
      oos.writeObject(updater);
    }
  }
Exemple #23
0
 /** Outputs the class index table to the INDEX.LIST file of the root jar file. */
 void dumpIndex(String rootjar, JarIndex index) throws IOException {
   File jarFile = new File(rootjar);
   Path jarPath = jarFile.toPath();
   Path tmpPath = createTempFileInSameDirectoryAs(jarFile).toPath();
   try {
     if (update(Files.newInputStream(jarPath), Files.newOutputStream(tmpPath), null, index)) {
       try {
         Files.move(tmpPath, jarPath, REPLACE_EXISTING);
       } catch (IOException e) {
         throw new IOException(getMsg("error.write.file"), e);
       }
     }
   } finally {
     Files.deleteIfExists(tmpPath);
   }
 }
Exemple #24
0
  @PreDestroy
  public void saveSession() {
    logger.debug("save session");
    try (OutputStream outStream =
        Files.newOutputStream(propPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
      props.store(outStream, name + " session properties");
    } catch (IOException ex) {
      logger.error(ex.getMessage(), ex);
    }

    try {
      saveHistory();
    } catch (IOException e) {
      logger.error(e.getMessage(), e);
    }
  }
Exemple #25
0
  void createArchive(Path projectPath, Path output, Config overrideParams) throws IOException {
    out.println("Creating " + output + "...");

    ProjectArchive project =
        projectLoader.load(projectPath, WorkflowResourceMatcher.defaultMatcher(), overrideParams);
    ArchiveMetadata meta = project.getArchiveMetadata();

    try (TarArchiveOutputStream tar =
        new TarArchiveOutputStream(new GzipCompressorOutputStream(Files.newOutputStream(output)))) {
      // default mode for file names longer than 100 bytes is throwing an exception (LONGFILE_ERROR)
      tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);

      project.listFiles(
          (resourceName, absPath) -> {
            if (!Files.isDirectory(absPath)) {
              out.println("  Archiving " + resourceName);

              TarArchiveEntry e = buildTarArchiveEntry(project, absPath, resourceName);
              tar.putArchiveEntry(e);
              if (e.isSymbolicLink()) {
                out.println("    symlink -> " + e.getLinkName());
              } else {
                try (InputStream in = Files.newInputStream(absPath)) {
                  ByteStreams.copy(in, tar);
                }
                tar.closeArchiveEntry();
              }
            }
          });

      // create .digdag.dig
      // TODO set default time zone if not set?
      byte[] metaBody = yamlMapper.toYaml(meta).getBytes(StandardCharsets.UTF_8);
      TarArchiveEntry metaEntry = new TarArchiveEntry(ArchiveMetadata.FILE_NAME);
      metaEntry.setSize(metaBody.length);
      metaEntry.setModTime(new Date());
      tar.putArchiveEntry(metaEntry);
      tar.write(metaBody);
      tar.closeArchiveEntry();
    }

    out.println("Workflows:");
    for (WorkflowDefinition workflow : meta.getWorkflowList().get()) {
      out.println("  " + workflow.getName());
    }
    out.println("");
  }
  private void createFavicon(Image img, Path outFile) throws IOException {
    int width = 32;
    int height = 32;
    String type = "png";

    Image thumb = img.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);

    BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);

    Graphics2D g = bimg.createGraphics();
    g.drawImage(thumb, 0, 0, null);
    g.dispose();

    try (OutputStream out = Files.newOutputStream(outFile)) {
      ImageIO.write(bimg, type, out);
    }
  }
Exemple #27
0
 @Override
 public void run() {
   Path file = entry.cacheFile;
   if (!Files.exists(file)) {
     try {
       Path parentDir = file.getParent();
       Files.createDirectories(parentDir);
       Path tmpFile = parentDir.resolve("." + file.getFileName().toString() + ".tmp");
       OutputStream tmpStream = Files.newOutputStream(tmpFile);
       tmpStream.write(entry.rawData);
       tmpStream.close();
       Files.move(tmpFile, file);
       logger.trace("Flushed: {}", file);
     } catch (IOException e) {
       logger.warn("Write failed: {}", file, e);
     }
   }
 }
 static String writeZip(Path structure, String prefix) throws IOException {
   Path zip = createTempDir().resolve(structure.getFileName() + ".zip");
   try (ZipOutputStream stream = new ZipOutputStream(Files.newOutputStream(zip))) {
     Files.walkFileTree(
         structure,
         new SimpleFileVisitor<Path>() {
           @Override
           public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
               throws IOException {
             String target =
                 (prefix == null ? "" : prefix + "/") + structure.relativize(file).toString();
             stream.putNextEntry(new ZipEntry(target));
             Files.copy(file, stream);
             return FileVisitResult.CONTINUE;
           }
         });
   }
   return zip.toUri().toURL().toString();
 }
Exemple #29
0
  private void saveHistory() throws IOException {
    if (history.isEmpty()) {
      Files.deleteIfExists(streamPath);
      return;
    }

    try (OutputStream outStream =
        Files.newOutputStream(
            streamPath,
            StandardOpenOption.CREATE,
            StandardOpenOption.WRITE,
            StandardOpenOption.TRUNCATE_EXISTING)) {
      try (ObjectOutputStream historyStream = new ObjectOutputStream(outStream)) {
        for (HistoryEntry h : history) {
          historyStream.writeObject(h);
        }
      }
    }
  }
  /** @param args */
  public static void main(String... args) {
    Thread.setDefaultUncaughtExceptionHandler(new LoggedUncaughtExceptionHandler());
    GigawordIngesterRunner run = new GigawordIngesterRunner();
    JCommander jc = new JCommander(run, args);
    jc.setProgramName(GigawordIngesterRunner.class.getSimpleName());
    if (run.delegate.help) {
      jc.usage();
    }

    try {
      Path outpath = Paths.get(run.delegate.outputPath);
      IngesterParameterDelegate.prepare(outpath);

      GigawordDocumentConverter conv = new GigawordDocumentConverter();
      for (String pstr : run.delegate.paths) {
        LOGGER.debug("Running on file: {}", pstr);
        Path p = Paths.get(pstr);
        new ExistingNonDirectoryFile(p);
        Path outWithExt = outpath.resolve(p.getFileName().toString().split("\\.")[0] + ".tar.gz");

        if (Files.exists(outWithExt)) {
          if (!run.delegate.overwrite) {
            LOGGER.info(
                "File: {} exists and overwrite disabled. Not running.", outWithExt.toString());
            continue;
          } else {
            Files.delete(outWithExt);
          }
        }

        try (OutputStream os = Files.newOutputStream(outWithExt);
            GzipCompressorOutputStream gout = new GzipCompressorOutputStream(os);
            TarArchiver arch = new TarArchiver(gout)) {
          Iterator<Communication> iter = conv.gzToStringIterator(p);
          while (iter.hasNext()) {
            arch.addEntry(new ArchivableCommunication(iter.next()));
          }
        }
      }
    } catch (NotFileException | IOException e) {
      LOGGER.error("Caught exception processing.", e);
    }
  }