FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaExercises/Experiments/ZipDirectory.java at master · biblelamp/JavaExercises · GitHub
biblelamp
/
JavaExercises
Public
Notifications
You must be signed in to change notification settings
Fork
130
Star
153
Code
Issues
1
Pull requests
9
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaExercises
/
Experiments
/
ZipDirectory.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
56 lines (48 loc) · 1.77 KB
Breadcrumbs
JavaExercises
/
Experiments
/
ZipDirectory.java
Copy path
File metadata and controls
56 lines (48 loc) · 1.77 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
java
.
io
.
File
;
import
java
.
io
.
FileInputStream
;
import
java
.
io
.
FileOutputStream
;
import
java
.
io
.
IOException
;
import
java
.
util
.
zip
.
ZipEntry
;
import
java
.
util
.
zip
.
ZipOutputStream
;
// @see https://www.baeldung.com/java-compress-and-uncompress
public
class
ZipDirectory
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
FileOutputStream
fos
=
new
FileOutputStream
(
"ZipFiles.zip"
);
ZipOutputStream
zipOut
=
new
ZipOutputStream
(
fos
);
File
fileToZip
;
fileToZip
=
new
File
(
"FileDrop"
);
zipFile
(
fileToZip
,
fileToZip
.
getName
(),
zipOut
);
fileToZip
=
new
File
(
"EncryptDecrypt"
);
zipFile
(
fileToZip
,
fileToZip
.
getName
(),
zipOut
);
zipOut
.
close
();
fos
.
close
();
}
private
static
void
zipFile
(
File
fileToZip
,
String
fileName
,
ZipOutputStream
zipOut
)
throws
IOException
{
if
(
fileToZip
.
isHidden
()) {
return
;
}
if
(
fileToZip
.
isDirectory
()) {
if
(
fileName
.
endsWith
(
"/"
)) {
zipOut
.
putNextEntry
(
new
ZipEntry
(
fileName
));
zipOut
.
closeEntry
();
}
else
{
zipOut
.
putNextEntry
(
new
ZipEntry
(
fileName
+
"/"
));
zipOut
.
closeEntry
();
}
File
[]
children
=
fileToZip
.
listFiles
();
for
(
File
childFile
:
children
) {
zipFile
(
childFile
,
fileName
+
"/"
+
childFile
.
getName
(),
zipOut
);
}
return
;
}
FileInputStream
fis
=
new
FileInputStream
(
fileToZip
);
ZipEntry
zipEntry
=
new
ZipEntry
(
fileName
);
zipOut
.
putNextEntry
(
zipEntry
);
byte
[]
bytes
=
new
byte
[
1024
];
int
length
;
while
((
length
=
fis
.
read
(
bytes
)) >=
0
) {
zipOut
.
write
(
bytes
,
0
,
length
);
}
fis
.
close
();
}
}
Back
|
FazBrowse Home
|
New Git URL