File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
Roadmap/41 - CAMISETA RAR/java Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .io .*;
2
+ import java .util .zip .ZipEntry ;
3
+ import java .util .zip .ZipOutputStream ;
4
+
5
+ public class Main {
6
+
7
+ public static void main (String [] args ) throws IOException {
8
+ var res = compressFile ("persons.csv" );
9
+ System .out .println (res ? "File compressed successfully" : "Failed to compress file" );
10
+ }
11
+
12
+ public static boolean compressFile (String sourceFile ) {
13
+ try (FileOutputStream fos = new FileOutputStream ("compressed.zip" );
14
+ ZipOutputStream zipOut = new ZipOutputStream (fos ) ) {
15
+
16
+ File fileToZip = new File (sourceFile );
17
+
18
+ try (FileInputStream fis = new FileInputStream (fileToZip )) {
19
+
20
+ ZipEntry zipEntry = new ZipEntry (fileToZip .getName ());
21
+ zipOut .putNextEntry (zipEntry );
22
+ byte [] bytes = new byte [2048 ];
23
+ int length ;
24
+
25
+ while ((length = fis .read (bytes )) >= 0 ) {
26
+ zipOut .write (bytes , 0 , length );
27
+ }
28
+
29
+ }
30
+ } catch (Exception e ) {
31
+ System .out .println (e .getMessage ());
32
+ return false ;
33
+ }
34
+
35
+ return true ;
36
+ }
37
+
38
+ }
You can’t perform that action at this time.
0 commit comments