Commit cc26a9cb authored by Robert Butora's avatar Robert Butora
Browse files

implements tar.gz with apache/commons-compress lib

parent eac0c8b7
Loading
Loading
Loading
Loading
+72 −3
Original line number Diff line number Diff line
@@ -25,6 +25,12 @@ import java.nio.file.Paths;
import java.time.*;// Timestamp in cut-filename
import java.io.ByteArrayOutputStream; // for SODA direct streaming doSubimgStream

// tar.gz compress
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import java.io.BufferedOutputStream;

import vo.parameter.*;

class DatasetsCli implements Datasets
@@ -116,6 +122,8 @@ class DatasetsCli implements Datasets
   private MCutResult doCompressCutFiles(MCutResult.Cut[] cutArr)
      throws IOException, InterruptedException
   {
      final String tgzFileName = settings.fitsPaths.cutouts() + "/mcutout_TIMESTAMP.tar.gz";
/*
      StringBuilder sb = new StringBuilder();
      for(MCutResult.Cut cut : cutArr)
      {
@@ -127,8 +135,6 @@ class DatasetsCli implements Datasets
         }
      }

      final String tgzFileName = settings.fitsPaths.cutouts() + "/mcutout_TIMESTAMP.tar.gz";

      String[] cmd = new String[6];
      cmd[0] = "/bin/tar";
      cmd[1] = "cfz";
@@ -148,13 +154,28 @@ class DatasetsCli implements Datasets
      LOGGER.info("exec tar.gz exitValue: " + exec.exitValue);

      boolean has_result = (exec.exitValue == 0);
*/
      List<Path> paths = new ArrayList<Path>();

      for(MCutResult.Cut cut : cutArr)
      {
         LOGGER.info("cut-id"+ String.valueOf(cut.index) + " -> " + cut.content);
         if(cut.contentType == MCutResult.Cut.ContentType.FILENAME)
         {
            Path p = Paths.get(cut.content);
            paths.add(p);
         }
      }

      Path output = Paths.get(tgzFileName);
      createTarGzipFiles(paths, output);

      MCutResult mCutResult = new MCutResult();
      mCutResult.cutResArr = cutArr;
      mCutResult.fileName = tgzFileName;
      mCutResult.fileSize = 0;

      // FIXME add timestamp, add filesize when successful
      // FIXME add response.json to tar.gz, add timestamp, add filesize when successful

      return mCutResult;
   }
@@ -200,5 +221,53 @@ class DatasetsCli implements Datasets
      return cut;
   }



   /*
      <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-compress</artifactId>
      <version>1.20</version>
      </dependency>

      import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
      import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
      import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;



*/
   public static void createTarGzipFiles(List<Path> paths, Path output)
         throws IOException
      {
         try (OutputStream fOut = Files.newOutputStream(output);
               BufferedOutputStream buffOut = new BufferedOutputStream(fOut);
               GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(buffOut);
               TarArchiveOutputStream tOut = new TarArchiveOutputStream(gzOut))
         {
            for (Path path : paths)
            {
               if (!Files.isRegularFile(path))
               {
                  throw new IOException("Must be regular file, but was : " + path.toString());
               }

               TarArchiveEntry tarEntry = new TarArchiveEntry(
                     path.toFile(),
                     path.getFileName().toString());

               tOut.putArchiveEntry(tarEntry);

               // copy file to TarArchiveOutputStream
               Files.copy(path, tOut);

               tOut.closeArchiveEntry();
            }

            tOut.finish();
         }

      }

}