Java Tutorial/File/ZipFile
Содержание
- 1 Create a simple ZIP File: not retain any directory path information about the files.
- 2 Create a zip file
- 3 Create checksum for a zip file
- 4 Extract file/files from a zip file
- 5 Get comment for a zipped file
- 6 Get CRC code for a zipped file
- 7 Get file time for all zipped files
- 8 Get uncompressed and compressed Size
- 9 Making a zip file of directory including its subdirectories recursively
- 10 Read a zip file checksum value
- 11 Read zip file
- 12 Retrieve a compressed file from a ZIP file
- 13 Use ZipFile to list all entries
- 14 Write Zip file
Create a simple ZIP File: not retain any directory path information about the files.
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Main {
static String rmPath(String fName) {
int pos = fName.lastIndexOf(File.separatorChar);
if (pos > -1)
fName = fName.substring(pos + 1);
return fName;
}
public static void main(String args[]) throws Exception {
ZipOutputStream fout = new ZipOutputStream(new BufferedOutputStream(
new FileOutputStream(args[0])));
for (int n = 1; n < args.length; n++) {
BufferedInputStream fin = new BufferedInputStream(new FileInputStream(
args[n]));
ZipEntry ze = new ZipEntry(rmPath(args[n]));
fout.putNextEntry(ze);
int i;
do {
i = fin.read();
if (i != -1)
fout.write(i);
} while (i != -1);
fout.closeEntry();
fin.close();
System.out.println("Compressing " + args[n]);
System.out.println(" Original Size: " + ze.getSize()
+ " Compressed Size: " + ze.getCompressedSize() + "\n");
}
fout.close();
}
}
Create a zip file
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Main {
public static void main(String[] args) throws Exception {
FileInputStream inStream = new FileInputStream("test.txt");
ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream("compressed.zip"));
outStream.putNextEntry(new ZipEntry("test.txt"));
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, bytesRead);
}
outStream.closeEntry();
outStream.close();
inStream.close();
}
}
Create checksum for a zip file
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Main {
public static void main(String[] args) throws Exception {
CheckedOutputStream checksum = new CheckedOutputStream(
new FileOutputStream("data.zip"), new Adler32());
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(checksum));
int size = 0;
byte[] buffer = new byte[1024];
File dir = new File(".");
String[] files = dir.list();
for (int i = 0; i < files.length; i++) {
System.out.println("Compressing: " + files[i]);
FileInputStream fis = new FileInputStream(files[i]);
ZipEntry zipEntry = new ZipEntry(files[i]);
zos.putNextEntry(zipEntry);
while ((size = fis.read(buffer, 0, buffer.length)) > 0) {
zos.write(buffer, 0, size);
}
zos.closeEntry();
fis.close();
}
zos.close();
System.out.println("Checksum : " + checksum.getChecksum().getValue());
}
}
Extract file/files from a zip file
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class Main {
public static void main(String[] args) throws Exception{
String destinationname = "d:\\";
byte[] buf = new byte[1024];
ZipInputStream zipinputstream = null;
ZipEntry zipentry;
zipinputstream = new ZipInputStream(new FileInputStream("fileName"));
zipentry = zipinputstream.getNextEntry();
while (zipentry != null) {
String entryName = zipentry.getName();
FileOutputStream fileoutputstream;
File newFile = new File(entryName);
String directory = newFile.getParent();
if (directory == null) {
if (newFile.isDirectory())
break;
}
fileoutputstream = new FileOutputStream(destinationname + entryName);
int n;
while ((n = zipinputstream.read(buf, 0, 1024)) > -1){
fileoutputstream.write(buf, 0, n);
}
fileoutputstream.close();
zipinputstream.closeEntry();
zipentry = zipinputstream.getNextEntry();
}
zipinputstream.close();
}
}
Get comment for a zipped file
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class MainClass {
public static void main(String[] args) {
try {
ZipFile zf = new ZipFile("your.zip");
Enumeration e = zf.entries();
while (e.hasMoreElements()) {
ZipEntry ze = (ZipEntry) e.nextElement();
String name = ze.getName();
long crc = ze.getCrc();
System.out.println("Its CRC is " + crc);
String comment = ze.getComment();
if (comment != null && !comment.equals("")) {
System.out.println(comment);
}
if (ze.isDirectory()) {
System.out.println(name + " is a directory");
}
}
} catch (IOException ex) {
System.err.println(ex);
}
}
}
Get CRC code for a zipped file
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class MainClass {
public static void main(String[] args) {
try {
ZipFile zf = new ZipFile("your.zip");
Enumeration e = zf.entries();
while (e.hasMoreElements()) {
ZipEntry ze = (ZipEntry) e.nextElement();
String name = ze.getName();
long crc = ze.getCrc();
System.out.println("Its CRC is " + crc);
String comment = ze.getComment();
if (comment != null && !comment.equals("")) {
System.out.println(comment);
}
if (ze.isDirectory()) {
System.out.println(name + " is a directory");
}
}
} catch (IOException ex) {
System.err.println(ex);
}
}
}
Get file time for all zipped files
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class MainClass {
public static void main(String[] args) {
try {
ZipFile zf = new ZipFile("your.zip");
Enumeration e = zf.entries();
while (e.hasMoreElements()) {
ZipEntry ze = (ZipEntry) e.nextElement();
String name = ze.getName();
Date lastModified = new Date(ze.getTime());
long uncompressedSize = ze.getSize();
long compressedSize = ze.getCompressedSize();
System.out.println(name);
System.out.println(lastModified);
System.out.println(uncompressedSize);
System.out.println(compressedSize);
}
} catch (IOException ex) {
System.err.println(ex);
}
}
}
Get uncompressed and compressed Size
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class MainClass {
public static void main(String[] args) {
try {
ZipFile zf = new ZipFile("your.zip");
Enumeration e = zf.entries();
while (e.hasMoreElements()) {
ZipEntry ze = (ZipEntry) e.nextElement();
String name = ze.getName();
Date lastModified = new Date(ze.getTime());
long uncompressedSize = ze.getSize();
long compressedSize = ze.getCompressedSize();
System.out.println(name);
System.out.println(lastModified);
System.out.println(uncompressedSize);
System.out.println(compressedSize);
}
} catch (IOException ex) {
System.err.println(ex);
}
}
}
Making a zip file of directory including its subdirectories recursively
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 Main {
private static void zipDir(String zipFileName, String dir) throws Exception {
File dirObj = new File(dir);
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
System.out.println("Creating : " + zipFileName);
addDir(dirObj, out);
out.close();
}
static void addDir(File dirObj, ZipOutputStream out) throws IOException {
File[] files = dirObj.listFiles();
byte[] tmpBuf = new byte[1024];
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
addDir(files[i], out);
continue;
}
FileInputStream in = new FileInputStream(files[i].getAbsolutePath());
System.out.println(" Adding: " + files[i].getAbsolutePath());
out.putNextEntry(new ZipEntry(files[i].getAbsolutePath()));
int len;
while ((len = in.read(tmpBuf)) > 0) {
out.write(tmpBuf, 0, len);
}
out.closeEntry();
in.close();
}
}
public static void main(String[] args) throws Exception {
zipDir(args[0], args[1]);
}
}
Read a zip file checksum value
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class Main {
public static void main(String[] args) throws Exception {
String zipname = "d.zip";
CheckedInputStream checksum = new CheckedInputStream(
new FileInputStream(zipname), new Adler32());
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum));
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
System.out.println("Unzipping: " + entry.getName());
int size;
byte[] buffer = new byte[2048];
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(entry.getName()), buffer.length);
while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, size);
}
bos.flush();
bos.close();
}
zis.close();
System.out.println("Checksum = " + checksum.getChecksum().getValue());
}
}
Read zip file
import java.io.FileInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class Main {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("C:/MyZip.zip");
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
System.out.println(ze.getName());
zis.closeEntry();
}
zis.close();
}
}
Retrieve a compressed file from a ZIP file
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.zip.ZipInputStream;
public class Main {
public static void main(String[] argv) throws Exception {
ZipInputStream in = new ZipInputStream(new FileInputStream("source.zip"));
OutputStream out = new FileOutputStream("target");
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
}
}
Use ZipFile to list all entries
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class MainClass {
public static void main(String[] args) throws IOException {
ZipFile zf = new ZipFile("test.zip");
Enumeration e = zf.entries();
while (e.hasMoreElements()) {
ZipEntry ze2 = (ZipEntry) e.nextElement();
System.out.println("File: " + ze2);
}
}
}
Write Zip file
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Main {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("C:/MyZip.zip");
ZipOutputStream zos = new ZipOutputStream(fos);
ZipEntry ze = new ZipEntry("C:/file1.txt");
zos.putNextEntry(ze);
zos.closeEntry();
ze = new ZipEntry("C:/file2.txt");
zos.putNextEntry(ze);
zos.closeEntry();
zos.close();
}
}