ZIP compressed input/output stream
ZIP compression management file is a very typical form of file compression. It can save storage space and implement the I/O stream of ZIP compression. It provides very useful related classes in the built-in Java classes, ZipOutputStream and ZipInputStream. To achieve file compression/decompression. Whether it is compressing or decompressing, a class is needed—ZipEntry class, which creates entries for compressed or decompressed files.
Compressed file
Using the ZipOutputStream class object, the file can be compressed into a .zip file. The process of compressing the file is
a common method of the ZipOutputStream class
method | Description |
---|---|
putNextEntry(ZipEntry e) | Start writing a new ZipEntry, and move the position in the stream to the beginning of the data pointed to by entry |
write(byte [] b, int off, int len) | Write byte array to current ZIP entry data |
finish() | Finish writing the content of the ZIP input stream without closing the OutputStream it cooperates with |
setComment(String comment) | You can set the comment text of this ZIP file |
code show as below:
public class Study2 {
public void zip() {
File f1 = new File("C:\\Users\\逢青\\Desktop\\123");// 压缩的源文件
File f2 = new File("C:\\Users\\逢青\\Desktop\\321.zip");// 压缩包位置
try (FileOutputStream fos = new FileOutputStream(f2); ZipOutputStream zos = new ZipOutputStream(fos)) {
if (f1.isDirectory()) {
// 判断源文件是否为文件夹
for (File f : f1.listFiles()) {
// 遍历文件夹中的文件
addEntry(zos, "", f);//调用创建方法的条目,为遍历的文件创建条目
}
} else {
addEntry(zos, "", f1);//调用创建方法的条目,为文件创建条目
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
* @param zos-压缩流
* @param path-文件在压缩包中的路径
* @param f1-压缩的文件
*/
public void addEntry(ZipOutputStream zos, String path, File f1) {
// 创建条目的方法
if (f1.isDirectory()) {
// 判断文件是否为文件夹
for (File file : f1.listFiles()) {
// 遍历文件夹
addEntry(zos, path + f1.getName() + File.separator, file);// 为文件夹下的文件创建条目
}
} else {
byte b[] = new byte[1024];// 创建缓冲区
try (FileInputStream fis = new FileInputStream(f1)) {
int i = -1;
zos.putNextEntry(new ZipEntry(path + f1.getName()));// 在压缩包中添加新条目
//将文件写入压缩包
while ((i = fis.read(b)) != -1) {
zos.write(b,0,i);
zos.flush();//刷新
}
zos.closeEntry();//关闭条目
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Study2 s = new Study2();
s.zip();
}
}
It should be noted that files and folders are different. When there is a folder in the compressed file, you need to traverse this folder and add entries for the files in the folder. If it is a file, you can directly add entries.
unzip files
The ZipInputStream class can read files in ZIP compression format, including compressed and uncompressed entries.
The process of decompressing files
. Common methods of the ZipInputStream class
method | Description |
---|---|
read(byte [] b, int off, int len) | Read the off offset position in the target b array, the length is len bytes |
available() | Determine whether the data specified by the current entry has been read. Return 0 after reading, return 1 if not reading |
closeEntry () | Close the current ZIP entry and locate the stream to read the next entry |
skip(long n) | Skip the specified number of bytes in the current ZIP entry |
getNextEntry() | Read the next ZipEntry and move the position in the stream to the beginning of the data pointed to by the entry |
createZipEntry(String name) | Create a new ZipEntry object with the specified name parameter |
code show as below:
public class Study {
static void decompression() {
File f1 = new File("C:\\Users\\逢青\\Desktop\\123");//目标文件夹
File f2 = new File("C:\\Users\\逢青\\Desktop\\321.zip");//解压的源文件
byte b[] = new byte[1024];//缓冲区
ZipEntry zn = null;
try(FileInputStream fis = new FileInputStream(f2);ZipInputStream zis = new ZipInputStream(fis)) {
while(true) {
zn = zis.getNextEntry();//获取条目
if(zn == null) {
//判断是否为空,空则结束
break;
}
if(zn.isDirectory()) {
//判断是否为文件夹,是则跳过
continue;
}
File f = new File(f1,zn.getName());
if(!f.getParentFile().exists()) {
//判断目标文件夹是否存在
f.getParentFile().mkdirs();//创建目标文件夹
}
int i = -1;
FileOutputStream fos = new FileOutputStream(f);
while((i = zis.read(b)) != -1) {
fos.write(b, 0, i);
fos.flush();
}
fos.close();
zis.closeEntry();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
decompression();
}
}