一、解Zip,使用java自带API
public static void main(String[] args) throws IOException {
ZipFile zipFile = new ZipFile(new File("d:\\zf\\z.zip"));
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()){
ZipEntry zipEntry = entries.nextElement();
if(zipEntry.isDirectory()){
File f = new File("d:\\zf\\" + zipEntry.getName());
if(!f.exists())
f.mkdirs();
}else {
File f = new File("d:\\zf\\" + zipEntry.getName());
if(!f.exists())
f.createNewFile();
InputStream is = zipFile.getInputStream(zipEntry);
OutputStream os = new FileOutputStream(f);
byte[] buf = new byte[2048];
int len;
while ((len=is.read(buf))!=-1)
os.write(buf, 0, len);
os.close();
is.close();
}
}
}
二、解Rar,使用依赖包com.github.junrar:junrar:3.0.0
public static void main(String[] args) throws Exception {
File outFileDir = new File("d:\\zf\\");
if (!outFileDir.exists()) {
boolean isMakDir = outFileDir.mkdirs();
if (isMakDir) {
System.out.println("创建压缩目录成功");
}
}
Archive archive = new Archive(new FileInputStream(new File("d:\\zf\\r.rar")));
FileHeader fileHeader = archive.nextFileHeader();
while (fileHeader != null) {
if (fileHeader.isDirectory()) {
fileHeader = archive.nextFileHeader();
continue;
}
File out = new File("d:\\zf\\" + fileHeader.getFileNameString());
if (!out.exists()) {
if (!out.getParentFile().exists()) {
out.getParentFile().mkdirs();
}
out.createNewFile();
}
FileOutputStream os = new FileOutputStream(out);
archive.extractFile(fileHeader, os);
os.close();
fileHeader = archive.nextFileHeader();
}
archive.close();
}
参考:有过参考,链接找不到了。。。蛋疼