예제 #1
0
  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);
  }
  public static void main(String[] args) throws IOException {

    int servPort = Integer.parseInt(args[0]);

    String ksName = "keystore.jks";
    char ksPass[] = "password".toCharArray();
    char ctPass[] = "password".toCharArray();
    try {
      KeyStore ks = KeyStore.getInstance("JKS");
      ks.load(new FileInputStream(ksName), ksPass);
      KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
      kmf.init(ks, ctPass);
      SSLContext sc = SSLContext.getInstance("SSL");
      sc.init(kmf.getKeyManagers(), null, null);
      SSLServerSocketFactory ssf = sc.getServerSocketFactory();
      SSLServerSocket s = (SSLServerSocket) ssf.createServerSocket(servPort);

      while (true) {
        // Listen for a TCP connection request.
        SSLSocket c = (SSLSocket) s.accept();

        BufferedOutputStream out = new BufferedOutputStream(c.getOutputStream(), 1024);
        BufferedInputStream in = new BufferedInputStream(c.getInputStream(), 1024);

        byte[] byteBuffer = new byte[1024];
        int count = 0;
        while ((byteBuffer[count] = (byte) in.read()) != -2) {
          count++;
        }
        String newFile = new String(byteBuffer, 0, count, "US-ASCII");
        FileOutputStream writer = new FileOutputStream(newFile.trim());
        int buffSize = 0;
        while ((buffSize = in.read(byteBuffer, 0, 1024)) != -1) {
          int index = 0;
          if ((index =
                  (new String(byteBuffer, 0, buffSize, "US-ASCII"))
                      .indexOf("------MagicStringCSE283Miami"))
              == -1) {
            writer.write(byteBuffer, 0, buffSize);
          } else {
            writer.write(byteBuffer, 0, index);
            break;
          }
        }
        writer.flush();
        writer.close();

        ZipOutputStream outZip = new ZipOutputStream(new BufferedOutputStream(out));
        FileInputStream fin = new FileInputStream(newFile.trim());
        BufferedInputStream origin = new BufferedInputStream(fin, 1024);
        ZipEntry entry = new ZipEntry(newFile.trim());
        outZip.putNextEntry(entry);

        byteBuffer = new byte[1024];
        int bytes = 0;
        while ((bytes = origin.read(byteBuffer, 0, 1024)) != -1) {
          outZip.write(byteBuffer, 0, bytes);
        }
        origin.close();
        outZip.flush();
        outZip.close();
        out.flush();
        out.close();
      }
    } catch (Exception e) {
      System.err.println(e.toString());
    }
  }