Get InputStream out of TarArchiveInputStream












0














I'm getting files from an FTP.
The files I'm getting are either text files or tar.gz



For text files I just send them to S3. If I encounter a tar.gz I want to untar it and save each file with the same method.



public void handleFile() {

try (InputStream fileStream = ftpFileService.getInputStream(file)) {
if (file.getName().lastIndexOf(".TGZ") > -1) {
TarArchiveInputStream tar = new TarArchiveInputStream(new GzipCompressorInputStream(fileStream));
TarArchiveEntry entry = null;
while ((entry = tar.getNextTarEntry()) != null) {
LOGGER.info("fileName to save {}", entry.getName());
saveStreamToS3(entry.getName(), new InputStream(tar));
}
tar.close();
} else {
LOGGER.info("fileName to save {}", fileName.getName());
saveStreamToS3(fileName.getName(), fileStream);
}
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
}


I tried saving the entry directly using new FileInputStream(entry.getFile()) but this returns null.



Do I need to make a saveTarStreamToS3() or can I make an InputStream out of a TarArchiveInputStream?










share|improve this question





























    0














    I'm getting files from an FTP.
    The files I'm getting are either text files or tar.gz



    For text files I just send them to S3. If I encounter a tar.gz I want to untar it and save each file with the same method.



    public void handleFile() {

    try (InputStream fileStream = ftpFileService.getInputStream(file)) {
    if (file.getName().lastIndexOf(".TGZ") > -1) {
    TarArchiveInputStream tar = new TarArchiveInputStream(new GzipCompressorInputStream(fileStream));
    TarArchiveEntry entry = null;
    while ((entry = tar.getNextTarEntry()) != null) {
    LOGGER.info("fileName to save {}", entry.getName());
    saveStreamToS3(entry.getName(), new InputStream(tar));
    }
    tar.close();
    } else {
    LOGGER.info("fileName to save {}", fileName.getName());
    saveStreamToS3(fileName.getName(), fileStream);
    }
    } catch (IOException e) {
    LOGGER.error(e.getMessage());
    }
    }


    I tried saving the entry directly using new FileInputStream(entry.getFile()) but this returns null.



    Do I need to make a saveTarStreamToS3() or can I make an InputStream out of a TarArchiveInputStream?










    share|improve this question



























      0












      0








      0







      I'm getting files from an FTP.
      The files I'm getting are either text files or tar.gz



      For text files I just send them to S3. If I encounter a tar.gz I want to untar it and save each file with the same method.



      public void handleFile() {

      try (InputStream fileStream = ftpFileService.getInputStream(file)) {
      if (file.getName().lastIndexOf(".TGZ") > -1) {
      TarArchiveInputStream tar = new TarArchiveInputStream(new GzipCompressorInputStream(fileStream));
      TarArchiveEntry entry = null;
      while ((entry = tar.getNextTarEntry()) != null) {
      LOGGER.info("fileName to save {}", entry.getName());
      saveStreamToS3(entry.getName(), new InputStream(tar));
      }
      tar.close();
      } else {
      LOGGER.info("fileName to save {}", fileName.getName());
      saveStreamToS3(fileName.getName(), fileStream);
      }
      } catch (IOException e) {
      LOGGER.error(e.getMessage());
      }
      }


      I tried saving the entry directly using new FileInputStream(entry.getFile()) but this returns null.



      Do I need to make a saveTarStreamToS3() or can I make an InputStream out of a TarArchiveInputStream?










      share|improve this question















      I'm getting files from an FTP.
      The files I'm getting are either text files or tar.gz



      For text files I just send them to S3. If I encounter a tar.gz I want to untar it and save each file with the same method.



      public void handleFile() {

      try (InputStream fileStream = ftpFileService.getInputStream(file)) {
      if (file.getName().lastIndexOf(".TGZ") > -1) {
      TarArchiveInputStream tar = new TarArchiveInputStream(new GzipCompressorInputStream(fileStream));
      TarArchiveEntry entry = null;
      while ((entry = tar.getNextTarEntry()) != null) {
      LOGGER.info("fileName to save {}", entry.getName());
      saveStreamToS3(entry.getName(), new InputStream(tar));
      }
      tar.close();
      } else {
      LOGGER.info("fileName to save {}", fileName.getName());
      saveStreamToS3(fileName.getName(), fileStream);
      }
      } catch (IOException e) {
      LOGGER.error(e.getMessage());
      }
      }


      I tried saving the entry directly using new FileInputStream(entry.getFile()) but this returns null.



      Do I need to make a saveTarStreamToS3() or can I make an InputStream out of a TarArchiveInputStream?







      java amazon-s3 apache-commons-compress






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 at 9:57

























      asked Nov 23 at 9:52









      gyc

      3,28431430




      3,28431430
























          1 Answer
          1






          active

          oldest

          votes


















          1














          FileInputStream only reads real files. It doesn't read data from inside an archive.



          There are two possible solutions




          • Use InputStream which FileInputStream and TarArchiveInputStream impliements

          • Copy the file to disk, read using FileInputStream, delete afterwards.


          The purpose on the interface InputStream is so you don't need to know where the data is coming from, and this is the natural way to solve this.




          can I make an InputStream out of a TarArchiveInputStream




          TarArchiveInputStream implements InputStream so there is nothing to do.






          share|improve this answer





















          • @gyc you don't need to cast it when upcasting. you only need to cast it when downcasting e.g. (TarArchiveInputStream) inputStream needs a cast.
            – Peter Lawrey
            Nov 23 at 10:05










          • Yes I understand, thank you
            – gyc
            Nov 23 at 10:20











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444291%2fget-inputstream-out-of-tararchiveinputstream%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          FileInputStream only reads real files. It doesn't read data from inside an archive.



          There are two possible solutions




          • Use InputStream which FileInputStream and TarArchiveInputStream impliements

          • Copy the file to disk, read using FileInputStream, delete afterwards.


          The purpose on the interface InputStream is so you don't need to know where the data is coming from, and this is the natural way to solve this.




          can I make an InputStream out of a TarArchiveInputStream




          TarArchiveInputStream implements InputStream so there is nothing to do.






          share|improve this answer





















          • @gyc you don't need to cast it when upcasting. you only need to cast it when downcasting e.g. (TarArchiveInputStream) inputStream needs a cast.
            – Peter Lawrey
            Nov 23 at 10:05










          • Yes I understand, thank you
            – gyc
            Nov 23 at 10:20
















          1














          FileInputStream only reads real files. It doesn't read data from inside an archive.



          There are two possible solutions




          • Use InputStream which FileInputStream and TarArchiveInputStream impliements

          • Copy the file to disk, read using FileInputStream, delete afterwards.


          The purpose on the interface InputStream is so you don't need to know where the data is coming from, and this is the natural way to solve this.




          can I make an InputStream out of a TarArchiveInputStream




          TarArchiveInputStream implements InputStream so there is nothing to do.






          share|improve this answer





















          • @gyc you don't need to cast it when upcasting. you only need to cast it when downcasting e.g. (TarArchiveInputStream) inputStream needs a cast.
            – Peter Lawrey
            Nov 23 at 10:05










          • Yes I understand, thank you
            – gyc
            Nov 23 at 10:20














          1












          1








          1






          FileInputStream only reads real files. It doesn't read data from inside an archive.



          There are two possible solutions




          • Use InputStream which FileInputStream and TarArchiveInputStream impliements

          • Copy the file to disk, read using FileInputStream, delete afterwards.


          The purpose on the interface InputStream is so you don't need to know where the data is coming from, and this is the natural way to solve this.




          can I make an InputStream out of a TarArchiveInputStream




          TarArchiveInputStream implements InputStream so there is nothing to do.






          share|improve this answer












          FileInputStream only reads real files. It doesn't read data from inside an archive.



          There are two possible solutions




          • Use InputStream which FileInputStream and TarArchiveInputStream impliements

          • Copy the file to disk, read using FileInputStream, delete afterwards.


          The purpose on the interface InputStream is so you don't need to know where the data is coming from, and this is the natural way to solve this.




          can I make an InputStream out of a TarArchiveInputStream




          TarArchiveInputStream implements InputStream so there is nothing to do.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 at 9:58









          Peter Lawrey

          441k55558959




          441k55558959












          • @gyc you don't need to cast it when upcasting. you only need to cast it when downcasting e.g. (TarArchiveInputStream) inputStream needs a cast.
            – Peter Lawrey
            Nov 23 at 10:05










          • Yes I understand, thank you
            – gyc
            Nov 23 at 10:20


















          • @gyc you don't need to cast it when upcasting. you only need to cast it when downcasting e.g. (TarArchiveInputStream) inputStream needs a cast.
            – Peter Lawrey
            Nov 23 at 10:05










          • Yes I understand, thank you
            – gyc
            Nov 23 at 10:20
















          @gyc you don't need to cast it when upcasting. you only need to cast it when downcasting e.g. (TarArchiveInputStream) inputStream needs a cast.
          – Peter Lawrey
          Nov 23 at 10:05




          @gyc you don't need to cast it when upcasting. you only need to cast it when downcasting e.g. (TarArchiveInputStream) inputStream needs a cast.
          – Peter Lawrey
          Nov 23 at 10:05












          Yes I understand, thank you
          – gyc
          Nov 23 at 10:20




          Yes I understand, thank you
          – gyc
          Nov 23 at 10:20


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444291%2fget-inputstream-out-of-tararchiveinputstream%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Lallio

          Unable to find Lightning Node

          Futebolista