-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileZipper.java
148 lines (136 loc) · 4.89 KB
/
FileZipper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.io.*;
import java.util.Objects;
import java.util.Scanner;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
public class FileZipper{
static final int BUFFER = 1024;
public static void main(String[] args){
//Constructor
while(true){
getOption();
}
}
private static void getOption(){
Scanner userInput = new Scanner(System.in);
System.out.println("Would you like to Zip or Unzip?");
String result = userInput.nextLine();
if(Objects.equals(result,"Zip")){
zipFile();
} else if (Objects.equals(result, "zip")) {
zipFile();
} else if (Objects.equals(result, "Unzip")) {
unzipFile();
} else if (Objects.equals(result, "unzip")) {
unzipFile();
}
}
private static void zipFile(){
ZipOutputStream zipOutputStream = null;
BufferedInputStream bufferedInputStream = null;
Scanner fileInput = new Scanner(System.in);
System.out.println("Enter the path of the file to zip");
String filePath = fileInput.nextLine();
try{
/*Can only put buffer variables in buffered stream objects.
If no buffered stream objects are used, then do not use the offset and len parameters in the
read() function.
*/
File file = new File(filePath);
FileInputStream fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream, BUFFER);
Scanner zipFileInput = new Scanner(System.in);
System.out.println("Enter a name for the zip archive");
String zipPath = zipFileInput.nextLine();
File zipFile = new File(zipPath);
FileOutputStream fileOutputStream = new FileOutputStream(zipFile); //Change this path everytime you move the folder
zipOutputStream = new ZipOutputStream(fileOutputStream);
ZipEntry zipEntry = new ZipEntry(file.getName()); //Declares specified file to the zip archive
zipOutputStream.putNextEntry(zipEntry); //Adds specified file to the zip archive
byte data[] = new byte[BUFFER];
int count;
while((count = bufferedInputStream.read(data, 0, BUFFER)) > 0){
zipOutputStream.write(data, 0, count);
}
System.out.println("Archived " + filePath + " to " + zipFile.getAbsolutePath());
try{
Thread.sleep(2000);
System.out.println("\n");
}catch (InterruptedException ex){
new RuntimeException(ex);
}
System.out.println("\n");
}catch(IOException IOError){
System.out.println("Error zipping file :" + IOError.getMessage());
} finally{
if(zipOutputStream != null){
try{
zipOutputStream.close();
}catch(IOException IOError){
IOError.printStackTrace();
}
}
if(bufferedInputStream != null){
try{
bufferedInputStream.close();
}catch(IOException IOError){
IOError.printStackTrace();
}
}
}
}
public static void unzipFile(){
Scanner zipLocation = new Scanner(System.in);
System.out.println("Enter path of archive to unzip");
String zipFilename = zipLocation.nextLine();
File zipFile = new File(zipFilename);
Scanner extractLocation = new Scanner(System.in);
System.out.println("Enter extraction location");
String destinationLocation = extractLocation.nextLine();
File destinationDirectory = new File(destinationLocation);
if(!destinationDirectory.exists()){
destinationDirectory.mkdirs();
}
try {
/*Can only put buffer variables in buffered stream objects.
If no buffered stream objects are used, then do not use the offset and len parameters in the
read() function.
*/
FileInputStream fileInputStream = new FileInputStream(zipFile);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream, BUFFER);
ZipInputStream zipInputStream = new ZipInputStream(bufferedInputStream);
ZipEntry zipEntry = zipInputStream.getNextEntry();
while (zipEntry != null) {
byte[] buffer = new byte[BUFFER];
String fileName = zipEntry.getName();
File newFile = new File(destinationLocation + File.separator + fileName);
System.out.println("Unzipping to " + newFile.getAbsolutePath());
new File(String.valueOf(newFile.getParentFile().mkdirs()));
FileOutputStream fileOutputStream = new FileOutputStream(newFile);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
int len = 0;
while ((len = zipInputStream.read(buffer, 0, len)) > 0) {
bufferedOutputStream.write(buffer, 0, len);
}
bufferedOutputStream.close();
zipInputStream.closeEntry();
zipEntry = zipInputStream.getNextEntry();
}
zipInputStream.closeEntry();
fileInputStream.close();
zipInputStream.close();
} catch (FileNotFoundException fileNotFoundException) {
System.out.println("Error unzipping file " + fileNotFoundException.getMessage());
destinationDirectory.delete();
try {
Thread.sleep(2000);
System.out.println("\n");
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}