Get InputStream out of TarArchiveInputStream
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
add a comment |
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
add a comment |
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
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
java amazon-s3 apache-commons-compress
edited Nov 23 at 9:57
asked Nov 23 at 9:52
gyc
3,28431430
3,28431430
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
FileInputStream only reads real files. It doesn't read data from inside an archive.
There are two possible solutions
- Use
InputStreamwhich 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.
@gyc you don't need to cast it when upcasting. you only need to cast it when downcasting e.g.(TarArchiveInputStream) inputStreamneeds a cast.
– Peter Lawrey
Nov 23 at 10:05
Yes I understand, thank you
– gyc
Nov 23 at 10:20
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
FileInputStream only reads real files. It doesn't read data from inside an archive.
There are two possible solutions
- Use
InputStreamwhich 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.
@gyc you don't need to cast it when upcasting. you only need to cast it when downcasting e.g.(TarArchiveInputStream) inputStreamneeds a cast.
– Peter Lawrey
Nov 23 at 10:05
Yes I understand, thank you
– gyc
Nov 23 at 10:20
add a comment |
FileInputStream only reads real files. It doesn't read data from inside an archive.
There are two possible solutions
- Use
InputStreamwhich 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.
@gyc you don't need to cast it when upcasting. you only need to cast it when downcasting e.g.(TarArchiveInputStream) inputStreamneeds a cast.
– Peter Lawrey
Nov 23 at 10:05
Yes I understand, thank you
– gyc
Nov 23 at 10:20
add a comment |
FileInputStream only reads real files. It doesn't read data from inside an archive.
There are two possible solutions
- Use
InputStreamwhich 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.
FileInputStream only reads real files. It doesn't read data from inside an archive.
There are two possible solutions
- Use
InputStreamwhich 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.
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) inputStreamneeds a cast.
– Peter Lawrey
Nov 23 at 10:05
Yes I understand, thank you
– gyc
Nov 23 at 10:20
add a comment |
@gyc you don't need to cast it when upcasting. you only need to cast it when downcasting e.g.(TarArchiveInputStream) inputStreamneeds 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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