Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1.  public void processClientsInfoFromZip(String nameArchive) throws IOException {
  2.         log.debug("Start work with the archive");
  3.         File inputArchiveFile = new File(path, nameArchive);
  4.         // создаем файл и записываем туда данные из файла в архиве
  5.         writeFile(inputArchiveFile);
  6.         //обработка данных по каждому клиенту и запись в кафку
  7.         processClientsInfo(inputArchiveFile);
  8.     }
  9.  
  10.     public void writeFile(File inputArchiveFile) throws IOException {
  11.         log.debug("Write to file");
  12.         //получаем файл из архива
  13.         File outputFile = getNewFileFromArchive(inputArchiveFile);
  14.         // BufferedWriter это буферизированная запись в файл, то есть пишем не побайтно
  15.         BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), StandardCharsets.UTF_8));
  16.         InputStream stream = getInputStream(inputArchiveFile);
  17.         for (int oneByte = stream.read(); oneByte != -1; oneByte = stream.read()) {
  18.             writer.write(oneByte);
  19.         }
  20.         writer.flush();
  21.     }
  22.  
  23.     public void processClientsInfo(File nameArchive) throws IOException {
  24.         log.debug("Process Clients Info");
  25.         // получение потока данных в файле
  26.         InputStream stream = getInputStream(nameArchive);
  27.         //BufferedReader Считывает текст из потока ввода символов, буферизует символы
  28.         //InputStreamReader это мост между символьным и байтовым потоками:
  29.         //он считывает байты и декодирует их в символы
  30.         BufferedReader reader1 = new BufferedReader(new InputStreamReader(stream));
  31.         CSVParser csvParser1 = new CSVParser(reader1, CSVFormat.DEFAULT.withFirstRecordAsHeader().withDelimiter(delimiter));
  32.         for (CSVRecord csvRecord1 : csvParser1) {
  33.             writeOneClientInfo(csvRecord1.toMap());
  34.         }
  35.     }
  36.  
  37.     public InputStream getInputStream(File inputArchiveFile) throws IOException {
  38.         ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(inputArchiveFile.getAbsolutePath()));
  39.         ZipEntry entry = zipInputStream.getNextEntry();
  40.         ZipFile zipFile = new ZipFile(inputArchiveFile);
  41.         Enumeration<? extends ZipEntry> entries = zipFile.entries();
  42.         entries.hasMoreElements();
  43.         InputStream stream = zipFile.getInputStream(entry);
  44.  
  45.         return stream;
  46.     }
  47.  
  48.     public File getNewFileFromArchive(File inputArchiveFile) throws IOException {
  49.         // ZipInputStream потоковое(побайтовое) чтение архива
  50.         ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(inputArchiveFile.getAbsolutePath()));
  51.         //getNextEntry функция, которая перемещает указатель на начало следующего файла в архиве
  52.         ZipEntry entry = zipInputStream.getNextEntry();
  53.         File outputFile = new File(newPath, entry.getName());
  54.         return outputFile;
  55.     }
  56.  
  57.     private void writeOneClientInfo(Map<String, String> clientRawInfo) {
  58.         log.debug("Write to kafka");
  59.         ClientInfo clientInfo = new ClientInfo(new LinkedHashMap<>(clientRawInfo));
  60.  
  61.         log.debug("Write JSON-messages to the Kafka topic, message:{}", clientInfo);
  62.  
  63.         kafkaTemplate.send(topic, clientInfo);
  64.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement