Example #1
0
  public static FileDesc loadFile(Path root, Path file, int blocSize)
      throws NoSuchAlgorithmException, FileNotFoundException, IOException {
    MessageDigest md = MessageDigest.getInstance("SHA-512");
    MessageDigest fileMd = MessageDigest.getInstance("SHA-512");

    FileDesc desc = new FileDesc(file.toString(), null, null);
    List<Bloc> list = new ArrayList<Bloc>();
    try (FileInputStream fis = new FileInputStream(root.resolve(file).toString())) {
      byte[] buf = new byte[blocSize];
      byte[] h;
      int s;
      while ((s = fis.read(buf)) != -1) {
        int c;
        while (s < buf.length && (c = fis.read()) != -1) buf[s++] = (byte) c;
        fileMd.update(buf, 0, s);

        // padding
        byte p = 0;
        while (s < buf.length) buf[s++] = ++p;
        h = md.digest(buf);
        Bloc bloc = new Bloc(RollingChecksum.compute(buf), new Hash(h));
        list.add(bloc);
      }
      h = fileMd.digest();
      desc.fileHash = new Hash(h);
      desc.blocs = list.toArray(new Bloc[0]);
    }
    return desc;
  }
Example #2
0
  public static void main(String args[]) {
    try {
      aServer asr = new aServer();

      // file channel.
      FileInputStream is = new FileInputStream("");
      is.read();
      FileChannel cha = is.getChannel();
      ByteBuffer bf = ByteBuffer.allocate(1024);
      bf.flip();

      cha.read(bf);

      // Path Paths
      Path pth = Paths.get("", "");

      // Files some static operation.
      Files.newByteChannel(pth);
      Files.copy(pth, pth);
      // file attribute, other different class for dos and posix system.
      BasicFileAttributes bas = Files.readAttributes(pth, BasicFileAttributes.class);
      bas.size();

    } catch (Exception e) {
      System.err.println(e);
    }

    System.out.println("hello ");
  }
  public void processInDirectory() {
    //
    // распакуем все файлы из каталага in
    //
    final int BUFFER_SIZE = 4096;
    SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
    final String sessionDate = df.format(new Date());
    //
    // ищем файлы с ответом
    logger.info("Обрабатываем входной каталог " + ABS_INPUT_DIR);
    try {
      File[] directoryList =
          (new File(ABS_INPUT_DIR))
              .listFiles(
                  pathname -> !pathname.isDirectory() && pathname.getName().endsWith(".zip"));
      //
      // распаковываем каждый zip
      //
      for (File curFile : directoryList) {
        logger.info("Распаковываем " + curFile.getName());
        //
        // открываем архив
        //
        FileInputStream fis = new FileInputStream(curFile);
        ZipInputStream zis = new ZipInputStream(fis);
        ZipEntry zipEntry;
        while ((zipEntry = zis.getNextEntry()) != null) {
          //
          // пропускаем директории, т.к. их быть не должно
          //
          if (zipEntry.isDirectory()) continue;
          //
          // из архива извлекаем только xml !!!
          //
          if (zipEntry.getName().endsWith(".xml")) {
            File unzippedFile = new File(ABS_INPUT_DIR + "/" + zipEntry.getName());
            logger.info("Извлекаем файл " + zipEntry.getName());
            FileOutputStream fos = new FileOutputStream(unzippedFile);
            byte[] buffer = new byte[BUFFER_SIZE];
            int count = 0;
            while ((count = zis.read(buffer)) > 0) {
              fos.write(buffer, 0, count);
            }
            fos.close();
          }
          zis.closeEntry();
        }
        zis.close();
        fis.close();
        Files.move(
            curFile.toPath(),
            Paths.get(ABS_ARCH_IN_DIR + "/" + sessionDate + "-" + curFile.getName()));
      }
    } catch (Exception e) {
      logger.error(e.getMessage());
      e.printStackTrace();
    }

    logger.info("Обработан входной каталог " + ABS_INPUT_DIR);
  }
Example #4
0
  private void storeFile(File file) throws FileNotFoundException, IOException {
    Console.printStatus("Storing: " + file);
    try (FileInputStream inputStream = new FileInputStream(file)) {
      _zipStream.putNextEntry(new ZipEntry(file.getCanonicalPath()));

      // Copy the content to the zip stream:

      int length;
      while ((length = inputStream.read(buffer)) > 0) _zipStream.write(buffer, 0, length);

      _zipStream.closeEntry();
    }
  }
 private void appendZipFile(ZipOutputStream dataZip, File file) throws Exception {
   logger.info("В архив записываем файл " + file.getName() + " -> " + file.getAbsolutePath());
   // открываем поток для чтения файла
   FileInputStream fis = new FileInputStream(file);
   byte[] buffer = new byte[fis.available()];
   fis.read(buffer);
   fis.close();
   // создаем элемент архива
   ZipEntry zipEntry = new ZipEntry(file.getName());
   // записываем данные в элемент архива и закрываем элемент
   dataZip.putNextEntry(zipEntry);
   dataZip.write(buffer);
   dataZip.closeEntry();
 }
Example #6
0
  private void laden(Path saveName) throws IOException {
    Properties prop = new Properties();

    FileInputStream in = new FileInputStream(saveName.toString());
    prop.load(in);

    for (int i = 0; prop.containsKey(String.format("quellMenu%d", i)); i++)
      quellListModel.addElement(
          new ListItem(
              Paths.get(prop.getProperty(String.format("quellMenu%d", i))),
              Paths.get(prop.getProperty(String.format("quellMenu%d", i)))));
    for (int i = 0; prop.containsKey(String.format("zielMenu%d", i)); i++)
      zielListModel.addElement(
          new ListItem(
              Paths.get(prop.getProperty(String.format("zielMenu%d", i))),
              Paths.get(prop.getProperty(String.format("zielMenu%d", i)))));

    in.close();
  }