add java zip examples
This commit is contained in:
parent
d64c9e47f4
commit
08c8d82cd4
@ -2,27 +2,26 @@ package com.baeldung.unzip;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipInputStream;
|
import java.util.zip.ZipInputStream;
|
||||||
|
|
||||||
public class UnzipFile {
|
public class UnzipFile {
|
||||||
public static void main(String[] args) throws FileNotFoundException, IOException {
|
public static void main(final String[] args) throws IOException {
|
||||||
String fileZip = "/opt/zipped/cities.zip";
|
final String fileZip = "src/main/resources/compressed.zip";
|
||||||
byte[] buffer = new byte[1024];
|
final byte[] buffer = new byte[1024];
|
||||||
ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip));
|
final ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip));
|
||||||
ZipEntry zipEntry = zis.getNextEntry();
|
ZipEntry zipEntry = zis.getNextEntry();
|
||||||
while(zipEntry != null){
|
while(zipEntry != null){
|
||||||
String fileName = zipEntry.getName();
|
final String fileName = zipEntry.getName();
|
||||||
File newFile = new File("/opt/unzipped/" + fileName);
|
final File newFile = new File("src/main/resources/unzipTest/" + fileName);
|
||||||
FileOutputStream fos = new FileOutputStream(newFile);
|
final FileOutputStream fos = new FileOutputStream(newFile);
|
||||||
int len;
|
int len;
|
||||||
while ((len = zis.read(buffer)) > 0) {
|
while ((len = zis.read(buffer)) > 0) {
|
||||||
fos.write(buffer, 0, len);
|
fos.write(buffer, 0, len);
|
||||||
}
|
}
|
||||||
fos.close();
|
fos.close();
|
||||||
zipEntry = zis.getNextEntry();
|
zipEntry = zis.getNextEntry();
|
||||||
}
|
}
|
||||||
zis.closeEntry();
|
zis.closeEntry();
|
||||||
|
43
core-java-8/src/main/java/com/baeldung/zip/ZipDirectory.java
Normal file
43
core-java-8/src/main/java/com/baeldung/zip/ZipDirectory.java
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package com.baeldung.zip;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
public class ZipDirectory {
|
||||||
|
public static void main(final String[] args) throws IOException {
|
||||||
|
final String sourceFile = "src/main/resources/zipTest";
|
||||||
|
final FileOutputStream fos = new FileOutputStream("src/main/resources/dirCompressed.zip");
|
||||||
|
final ZipOutputStream zipOut = new ZipOutputStream(fos);
|
||||||
|
final File fileToZip = new File(sourceFile);
|
||||||
|
|
||||||
|
zipFile(fileToZip, fileToZip.getName(), zipOut);
|
||||||
|
zipOut.close();
|
||||||
|
fos.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void zipFile(final File fileToZip, final String fileName, final ZipOutputStream zipOut) throws IOException {
|
||||||
|
if (fileToZip.isHidden()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (fileToZip.isDirectory()) {
|
||||||
|
final File[] children = fileToZip.listFiles();
|
||||||
|
for (final File childFile : children) {
|
||||||
|
zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final FileInputStream fis = new FileInputStream(fileToZip);
|
||||||
|
final ZipEntry zipEntry = new ZipEntry(fileName);
|
||||||
|
zipOut.putNextEntry(zipEntry);
|
||||||
|
final byte[] bytes = new byte[1024];
|
||||||
|
int length;
|
||||||
|
while ((length = fis.read(bytes)) >= 0) {
|
||||||
|
zipOut.write(bytes, 0, length);
|
||||||
|
}
|
||||||
|
fis.close();
|
||||||
|
}
|
||||||
|
}
|
@ -2,22 +2,21 @@ package com.baeldung.zip;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipOutputStream;
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
public class ZipFile {
|
public class ZipFile {
|
||||||
public static void main(String[] args) throws FileNotFoundException, IOException {
|
public static void main(final String[] args) throws IOException {
|
||||||
String sourceFile = "/opt/photos/photo.png";
|
final String sourceFile = "src/main/resources/zipTest/test1.txt";
|
||||||
FileOutputStream fos = new FileOutputStream("/opt/zipped/cities.zip");
|
final FileOutputStream fos = new FileOutputStream("src/main/resources/compressed.zip");
|
||||||
ZipOutputStream zipOut = new ZipOutputStream(fos);
|
final ZipOutputStream zipOut = new ZipOutputStream(fos);
|
||||||
File fileToZip = new File(sourceFile);
|
final File fileToZip = new File(sourceFile);
|
||||||
FileInputStream fis = new FileInputStream(fileToZip);
|
final FileInputStream fis = new FileInputStream(fileToZip);
|
||||||
ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
|
final ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
|
||||||
zipOut.putNextEntry(zipEntry);
|
zipOut.putNextEntry(zipEntry);
|
||||||
byte[] bytes = new byte[1024];
|
final byte[] bytes = new byte[1024];
|
||||||
int length;
|
int length;
|
||||||
while((length = fis.read(bytes)) >= 0) {
|
while((length = fis.read(bytes)) >= 0) {
|
||||||
zipOut.write(bytes, 0, length);
|
zipOut.write(bytes, 0, length);
|
||||||
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.baeldung.zip;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
|
public class ZipMultipleFiles {
|
||||||
|
public static void main(final String[] args) throws IOException {
|
||||||
|
final List<String> srcFiles = Arrays.asList("src/main/resources/zipTest/test1.txt", "src/main/resources/zipTest/test2.txt");
|
||||||
|
final FileOutputStream fos = new FileOutputStream("src/main/resources/multiCompressed.zip");
|
||||||
|
final ZipOutputStream zipOut = new ZipOutputStream(fos);
|
||||||
|
for (final String srcFile : srcFiles) {
|
||||||
|
final File fileToZip = new File(srcFile);
|
||||||
|
final FileInputStream fis = new FileInputStream(fileToZip);
|
||||||
|
final ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
|
||||||
|
zipOut.putNextEntry(zipEntry);
|
||||||
|
|
||||||
|
final byte[] bytes = new byte[1024];
|
||||||
|
int length;
|
||||||
|
while((length = fis.read(bytes)) >= 0) {
|
||||||
|
zipOut.write(bytes, 0, length);
|
||||||
|
}
|
||||||
|
fis.close();
|
||||||
|
}
|
||||||
|
zipOut.close();
|
||||||
|
fos.close();
|
||||||
|
}
|
||||||
|
}
|
BIN
core-java-8/src/main/resources/compressed.zip
Normal file
BIN
core-java-8/src/main/resources/compressed.zip
Normal file
Binary file not shown.
BIN
core-java-8/src/main/resources/dirCompressed.zip
Normal file
BIN
core-java-8/src/main/resources/dirCompressed.zip
Normal file
Binary file not shown.
BIN
core-java-8/src/main/resources/multiCompressed.zip
Normal file
BIN
core-java-8/src/main/resources/multiCompressed.zip
Normal file
Binary file not shown.
1
core-java-8/src/main/resources/unzipTest/test1.txt
Normal file
1
core-java-8/src/main/resources/unzipTest/test1.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
Hello World!
|
1
core-java-8/src/main/resources/zipTest/test1.txt
Normal file
1
core-java-8/src/main/resources/zipTest/test1.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
Hello World!
|
1
core-java-8/src/main/resources/zipTest/test2.txt
Normal file
1
core-java-8/src/main/resources/zipTest/test2.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
My Name is John
|
@ -0,0 +1 @@
|
|||||||
|
My Name is Tom
|
@ -0,0 +1 @@
|
|||||||
|
My Name is Jane
|
Loading…
x
Reference in New Issue
Block a user