1.读取压缩文件
zip文件
ZipInputStream的基本用法:
首先要创建一个ZipInputStream,通常是传入一个FileInputStream作为数据源,然后,循环调用getNextEntry(),直到返回null,表示zip流结束。ZipInputStream是一种FilterInputStream,它可以直接读取zip包的内容:
一个ZipEntry表示一个压缩文件或目录,如果是压缩文件,我们就用read()方法不断读取,直到返回-1:
//解压缩zip
private static void unzip(String path) {
File sourceFile=new File(path);
//根目录
String sourceFileName=sourceFile.getName();
//父地址+\\+名字中0到.
File rootDir =new File(sourceFile.getParent()+"\\"+sourceFileName.substring(0,sourceFileName.lastIndexOf(".")));
//判断目录是否存在
if (rootDir.exists()) {
try {
FileUtils.deleteDirectory(rootDir);
} catch (IOException e) {
e.printStackTrace();
}
}
//创建根目录
rootDir.mkdir();
try (ZipInputStream in=new ZipInputStream(new FileInputStream(sourceFile))){
//遍历子目录,子文件(ZipEntry类型的对象)
ZipEntry zipEntry=null;
while((zipEntry=in.getNextEntry())!=null) {
//创建子目录(File对象)
File file=new File(rootDir.getPath()+"\\"+zipEntry.getName());
if (zipEntry.isDirectory()) {
//物理磁盘创建子目录
file.mkdir();
}else {
file.createNewFile();
//读取当前压缩包中的子文件,并通过输出流out写入新子文件中
try (FileOutputStream out=new FileOutputStream(file);){
byte[] buff=new byte[1024];
int len=-1;
while ((len=in.read(buff))!=-1) {
out.write(buff,0,len);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
rar文件
//解压缩rar
private static void unrar(String path) {
//创建根目录
File rarFile=new File(path);
File rootDir=new File(rarFile.getParent()+"\\"+rarFile.getName().substring(0,rarFile.getName().lastIndexOf(".")));
if (rootDir.exists()) {
try {
FileUtils.deleteDirectory(rootDir);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
rootDir.mkdir();
//创建Archive对象,用于读取rar压缩文件格式
try (Archive archive=new Archive(new FileInputStream(path))){
//获取压缩文件中的所以子文件,子目录(FileHeader对象)
List<FileHeader> fileheaderList=archive.getFileHeaders();
//按照目录名称排序
fileheaderList.sort(new Comparator<FileHeader>() {
@Override
public int compare(FileHeader o1, FileHeader o2) {
// TODO Auto-generated method stub
return o1.getFileName().compareTo(o2.getFileName());
}
});
//遍历子目录,子文件
for (FileHeader fd:fileheaderList) {
File f=new File(rootDir.getPath()+"\\"+fd.getFileName());
if(fd.isDirectory()) {
//创建子目录
f.mkdir();
}else {
//创建子文件
f.createNewFile();
//获取压缩包中的子文件输入流
InputStream in =archive.getInputStream(fd);
//复制文件输入流至新子文件
FileUtils.copyInputStreamToFile(in, f);
}
}
} catch (RarException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
2.创建压缩包
ZipOutputStream是一种FilterOutputStream,它可以直接写入内容到zip包。我们要先创建一个ZipOutputStream,通常是包装一个FileOutputStream,然后,每写入一个文件前,先调用putNextEntry(),然后用write()写入byte[]数据,写入完毕后调用closeEntry()结束这个文件的打包。
public static void main(String[] args) {
File dir=new File(....);
File[] files=dir.listFiles();
//创建zip压缩文件输出流
try (ZipOutputStream out = new ZipOutputStream(
new FileOutputStream(dir.getPath()+"\\"+dir.getName()+".zip"))) {
for (File f:files) {
//写入一个压缩包中的子文件(ZipEntry对象)
out.putNextEntry(new ZipEntry(f.getName()));
//写入字节内容
out.write(Files.readAllBytes(f.toPath()));
//关闭压缩包中的子文件
out.closeEntry();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}