66import java .io .BufferedInputStream ;
77import java .io .ByteArrayInputStream ;
88import java .io .ByteArrayOutputStream ;
9- import java .io .File ;
10- import java .io .FileInputStream ;
11- import java .io .FileOutputStream ;
129import java .io .IOException ;
1310import java .io .InputStream ;
1411import java .io .OutputStream ;
12+ import java .nio .file .Files ;
13+ import java .nio .file .Path ;
14+ import java .nio .file .StandardCopyOption ;
1515import java .util .zip .ZipEntry ;
1616import java .util .zip .ZipInputStream ;
1717import java .util .zip .ZipOutputStream ;
3737 */
3838
3939public final class ZipHelper {
40-
41- private static final int BUFFER_SIZE = 2048 ;
42-
4340 @ FunctionalInterface
4441 public interface InputStreamConsumer {
4542 void acceptStream (InputStream inputStream ) throws IOException ;
@@ -55,17 +52,17 @@ private ZipHelper() { }
5552
5653
5754 /**
58- * This function helps to create zip files. Caution this will overwrite the original file.
55+ * This function helps to create zip files. Caution, this will overwrite the original file.
5956 *
6057 * @param outZip the ZipOutputStream where the data should be stored in
6158 * @param nameInZip the path of the file inside the zip
62- * @param fileOnDisk the path of the file on the disk that should be added to zip
59+ * @param path the path of the file on the disk that should be added to zip
6360 */
6461 public static void addFileToZip (final ZipOutputStream outZip ,
6562 final String nameInZip ,
66- final String fileOnDisk ) throws IOException {
67- try (FileInputStream fi = new FileInputStream ( fileOnDisk )) {
68- addFileToZip (outZip , nameInZip , fi );
63+ final Path path ) throws IOException {
64+ try (var inputStream = Files . newInputStream ( path )) {
65+ addFileToZip (outZip , nameInZip , inputStream );
6966 }
7067 }
7168
@@ -80,13 +77,13 @@ public static void addFileToZip(final ZipOutputStream outZip,
8077 final String nameInZip ,
8178 final OutputStreamConsumer streamConsumer ) throws IOException {
8279 final byte [] bytes ;
83- try (ByteArrayOutputStream byteOutput = new ByteArrayOutputStream ()) {
80+ try (var byteOutput = new ByteArrayOutputStream ()) {
8481 streamConsumer .acceptStream (byteOutput );
8582 bytes = byteOutput .toByteArray ();
8683 }
8784
88- try (ByteArrayInputStream byteInput = new ByteArrayInputStream (bytes )) {
89- ZipHelper . addFileToZip (outZip , nameInZip , byteInput );
85+ try (var byteInput = new ByteArrayInputStream (bytes )) {
86+ addFileToZip (outZip , nameInZip , byteInput );
9087 }
9188 }
9289
@@ -97,49 +94,26 @@ public static void addFileToZip(final ZipOutputStream outZip,
9794 * @param nameInZip the path of the file inside the zip
9895 * @param inputStream the content to put inside the file
9996 */
100- public static void addFileToZip (final ZipOutputStream outZip ,
101- final String nameInZip ,
102- final InputStream inputStream ) throws IOException {
103- final byte [] data = new byte [BUFFER_SIZE ];
104- try (BufferedInputStream bufferedInputStream =
105- new BufferedInputStream (inputStream , BUFFER_SIZE )) {
106- final ZipEntry entry = new ZipEntry (nameInZip );
107- outZip .putNextEntry (entry );
108- int count ;
109- while ((count = bufferedInputStream .read (data , 0 , BUFFER_SIZE )) != -1 ) {
110- outZip .write (data , 0 , count );
111- }
112- }
97+ private static void addFileToZip (final ZipOutputStream outZip ,
98+ final String nameInZip ,
99+ final InputStream inputStream ) throws IOException {
100+ outZip .putNextEntry (new ZipEntry (nameInZip ));
101+ inputStream .transferTo (outZip );
113102 }
114103
115104 /**
116- * This will extract data from ZipInputStream. Caution this will overwrite the original file.
105+ * This will extract data from ZipInputStream. Caution, this will overwrite the original file.
117106 *
118107 * @param zipFile the zip file to extract from
119108 * @param nameInZip the path of the file inside the zip
120- * @param fileOnDisk the path of the file on the disk where the data should be extracted to
109+ * @param path the path of the file on the disk where the data should be extracted to
121110 * @return will return true if the file was found within the zip file
122111 */
123112 public static boolean extractFileFromZip (final StoredFileHelper zipFile ,
124113 final String nameInZip ,
125- final String fileOnDisk ) throws IOException {
126- return extractFileFromZip (zipFile , nameInZip , input -> {
127- // delete old file first
128- final File oldFile = new File (fileOnDisk );
129- if (oldFile .exists ()) {
130- if (!oldFile .delete ()) {
131- throw new IOException ("Could not delete " + fileOnDisk );
132- }
133- }
134-
135- final byte [] data = new byte [BUFFER_SIZE ];
136- try (FileOutputStream outFile = new FileOutputStream (fileOnDisk )) {
137- int count ;
138- while ((count = input .read (data )) != -1 ) {
139- outFile .write (data , 0 , count );
140- }
141- }
142- });
114+ final Path path ) throws IOException {
115+ return extractFileFromZip (zipFile , nameInZip , input ->
116+ Files .copy (input , path , StandardCopyOption .REPLACE_EXISTING ));
143117 }
144118
145119 /**
0 commit comments