mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2025-11-15 14:47:11 +00:00
Use InputStream#transferTo()
This commit is contained in:
@@ -37,9 +37,6 @@ import java.util.zip.ZipOutputStream;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public final class ZipHelper {
|
public final class ZipHelper {
|
||||||
|
|
||||||
private static final int BUFFER_SIZE = 2048;
|
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface InputStreamConsumer {
|
public interface InputStreamConsumer {
|
||||||
void acceptStream(InputStream inputStream) throws IOException;
|
void acceptStream(InputStream inputStream) throws IOException;
|
||||||
@@ -80,13 +77,13 @@ public final class ZipHelper {
|
|||||||
final String nameInZip,
|
final String nameInZip,
|
||||||
final OutputStreamConsumer streamConsumer) throws IOException {
|
final OutputStreamConsumer streamConsumer) throws IOException {
|
||||||
final byte[] bytes;
|
final byte[] bytes;
|
||||||
try (ByteArrayOutputStream byteOutput = new ByteArrayOutputStream()) {
|
try (var byteOutput = new ByteArrayOutputStream()) {
|
||||||
streamConsumer.acceptStream(byteOutput);
|
streamConsumer.acceptStream(byteOutput);
|
||||||
bytes = byteOutput.toByteArray();
|
bytes = byteOutput.toByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
try (ByteArrayInputStream byteInput = new ByteArrayInputStream(bytes)) {
|
try (var byteInput = new ByteArrayInputStream(bytes)) {
|
||||||
ZipHelper.addFileToZip(outZip, nameInZip, byteInput);
|
addFileToZip(outZip, nameInZip, byteInput);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,19 +94,12 @@ public final class ZipHelper {
|
|||||||
* @param nameInZip the path of the file inside the zip
|
* @param nameInZip the path of the file inside the zip
|
||||||
* @param inputStream the content to put inside the file
|
* @param inputStream the content to put inside the file
|
||||||
*/
|
*/
|
||||||
public static void addFileToZip(final ZipOutputStream outZip,
|
private static void addFileToZip(final ZipOutputStream outZip,
|
||||||
final String nameInZip,
|
final String nameInZip,
|
||||||
final InputStream inputStream) throws IOException {
|
final InputStream inputStream) throws IOException {
|
||||||
final byte[] data = new byte[BUFFER_SIZE];
|
final var entry = new ZipEntry(nameInZip);
|
||||||
try (BufferedInputStream bufferedInputStream =
|
outZip.putNextEntry(entry);
|
||||||
new BufferedInputStream(inputStream, BUFFER_SIZE)) {
|
inputStream.transferTo(outZip);
|
||||||
final ZipEntry entry = new ZipEntry(nameInZip);
|
|
||||||
outZip.putNextEntry(entry);
|
|
||||||
int count;
|
|
||||||
while ((count = bufferedInputStream.read(data, 0, BUFFER_SIZE)) != -1) {
|
|
||||||
outZip.write(data, 0, count);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user