java 文件压缩和解压

引言

  • 文件压缩和解压是常见的操作
  • 本文重点总结了java中文件压缩和解压的方法

方法一

  • zip格式,不支持中文
  • 压缩:
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
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class App
{

public static void main( String[] args )
{
byte[] buffer = new byte[1024];

try{

FileOutputStream fos = new FileOutputStream("C:\\MyFile.zip");
ZipOutputStream zos = new ZipOutputStream(fos);
ZipEntry ze= new ZipEntry("spy.log");
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream("C:\\spy.log");

int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}

in.close();
zos.closeEntry();

//remember close it
zos.close();

System.out.println("Done");

}catch(IOException ex){
ex.printStackTrace();
}
}
}
  • 解压
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
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class UnZip
{
List<String> fileList;
private static final String INPUT_ZIP_FILE = "C:\\MyFile.zip";
private static final String OUTPUT_FOLDER = "C:\\outputzip";

public static void main( String[] args )
{
UnZip unZip = new UnZip();
unZip.unZipIt(INPUT_ZIP_FILE,OUTPUT_FOLDER);
}

/**
* Unzip it
* @param zipFile input zip file
* @param output zip file output folder
*/

public void unZipIt(String zipFile, String outputFolder){

byte[] buffer = new byte[1024];

try{

//create output directory is not exists
File folder = new File(OUTPUT_FOLDER);
if(!folder.exists()){
folder.mkdir();
}

//get the zip file content
ZipInputStream zis =
new ZipInputStream(new FileInputStream(zipFile));
//get the zipped file list entry
ZipEntry ze = zis.getNextEntry();

while(ze!=null){

String fileName = ze.getName();
File newFile = new File(outputFolder + File.separator + fileName);

System.out.println("file unzip : "+ newFile.getAbsoluteFile());

//create all non exists folders
//else you will hit FileNotFoundException for compressed folder
new File(newFile.getParent()).mkdirs();

FileOutputStream fos = new FileOutputStream(newFile);

int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}

fos.close();
ze = zis.getNextEntry();
}

zis.closeEntry();
zis.close();

System.out.println("Done");

}catch(IOException ex){
ex.printStackTrace();
}
}
}

方法二

  • GBK编码,支持中文,zip格式
  • 压缩
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
import java.io.File; 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class zip {

public static void main(String args[]) throws IOException {
File inFile = new File("D:\\test.txt");
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("D:\\test.zip"), Charset.forName("GBK"));

zos.setComment("ziped by Ming Cheng.");
zipFile(inFile, zos, "");
zos.close();
}

public static void zipFile(File inFile, ZipOutputStream zos, String dir) throws IOException {
if (inFile.isDirectory()) {
File[] files = inFile.listFiles();
for (File file:files)
zipFile(file, zos, dir + "\\" + inFile.getName());
} else {
String entryName = null;
if (!"".equals(dir))
entryName = dir + "\\" + inFile.getName();
else
entryName = inFile.getName();

ZipEntry entry = new ZipEntry(entryName);
zos.putNextEntry(entry);

InputStream is = new FileInputStream(inFile);
int len = 0;
while ((len = is.read()) != -1)
zos.write(len);

is.close();
}
}
}
  • 解压
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
import java.io.File; 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class Unzip {

public static void main(String args[]) throws IOException {
File file = new File("D:\\test.zip");
ZipFile zipFile = new ZipFile(file, Charset.forName("GBK"));

ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(file), Charset.forName("GBK"));
unpack(zipFile, zipInputStream);
}

public static void unpack(ZipFile zipFile, ZipInputStream zipInputStream) throws IOException {

ZipEntry zipEntry = null;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
String fileName = zipEntry.getName();
File temp = new File("D:\\Test\\" + fileName);

if (! temp.getParentFile().exists())
temp.getParentFile().mkdirs();

OutputStream os = new FileOutputStream(temp);
InputStream is = zipFile.getInputStream(zipEntry);

int len = 0;
System.out.println(zipEntry.getName());

while ((len = is.read()) != -1)
os.write(len);

os.close();
is.close();
}
zipInputStream.close();
}
}
您的支持是对我最大的鼓励!