package com.zuidaima.main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import com.github.junrar.Archive;
import com.github.junrar.exception.RarException;
import com.github.junrar.rarfile.FileHeader; /**
**/
public class Main { public void extractZipFiles() {
String root ="c:/javazip/";
URL url = Main.class.getResource("/aa.zip");
String filename = url.getFile();
try {
// destination folder to extract the contents
byte[] buf = new byte[1024];
ZipInputStream zipinputstream = null;
ZipEntry zipentry;
zipinputstream = new ZipInputStream(new FileInputStream(filename));
zipentry = zipinputstream.getNextEntry();
while (zipentry != null) { // for each entry to be extracted
String entryName = zipentry.getName(); System.out.println(entryName); int n;
FileOutputStream fileoutputstream;
File newFile = new File(entryName); String directory = newFile.getParent(); // to creating the parent directories
if (directory == null) {
if (newFile.isDirectory()) {
break;
}
} else {
new File(root + directory).mkdirs();
} if (!zipentry.isDirectory()) {
System.out.println("File to be extracted....."+ entryName);
fileoutputstream = new FileOutputStream(root + entryName);
while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
fileoutputstream.write(buf, 0, n);
}
fileoutputstream.close();
} zipinputstream.closeEntry();
zipentry = zipinputstream.getNextEntry();
}
zipinputstream.close();
} catch (Exception e) {
e.printStackTrace();
}
} public void extractRarFiles() {
URL url = Main.class.getResource("/bb.rar");
String filename = url.getFile();
File archive = new File(filename);
File destination = new File("c:/javazip/"); Archive arch = null;
try {
arch = new Archive(archive);
} catch (RarException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (arch != null) {
if (arch.isEncrypted()) {
System.out.println("archive is encrypted cannot extreact");
return;
}
FileHeader fh = null;
while (true) {
fh = arch.nextFileHeader();
if (fh == null) {
break;
}
if (fh.isEncrypted()) {
System.out.println("file is encrypted cannot extract:"
+ fh.getFileNameString());
continue;
}
System.out.println("extracting:"+ fh.getFileNameString());
try {
if (fh.isDirectory()) {
createDirectory(fh, destination);
} else {
File f = createFile(fh, destination);
OutputStream stream = new FileOutputStream(f);
arch.extractFile(fh, stream);
stream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
} private File createFile(FileHeader fh, File destination) {
File f = null;
String name = null;
if (fh.isFileHeader() && fh.isUnicode()) {
name = fh.getFileNameW();
} else {
name = fh.getFileNameString();
}
f = new File(destination, name);
if (!f.exists()) {
try {
f = makeFile(destination, name);
} catch (IOException e) {
e.printStackTrace();
}
}
return f;
} private File makeFile(File destination, String name) throws IOException {
String[] dirs = name.split("\");
if (dirs == null) {
return null;
}
String path ="";
int size = dirs.length;
if (size == 1) {
return new File(destination, name);
} else if (size > 1) {
for (int i = 0; i < dirs.length - 1; i++) {
path = path + File.separator + dirs[i];
new File(destination, path).mkdir();
}
path = path + File.separator + dirs[dirs.length - 1];
File f = new File(destination, path);
f.createNewFile();
return f;
} else {
return null;
}
} private void createDirectory(FileHeader fh, File destination) {
File f = null;
if (fh.isDirectory() && fh.isUnicode()) {
f = new File(destination, fh.getFileNameW());
if (!f.exists()) {
makeDirectory(destination, fh.getFileNameW());
}
} else if (fh.isDirectory() && !fh.isUnicode()) {
f = new File(destination, fh.getFileNameString());
if (!f.exists()) {
makeDirectory(destination, fh.getFileNameString());
}
}
} private void makeDirectory(File destination, String fileName) {
String[] dirs = fileName.split("\");
if (dirs == null) {
return;
}
String path ="";
for (String dir : dirs) {
path = path + File.separator + dir;
new File(destination, path).mkdir();
}
} public static void main(String[] args) {
Main main = new Main();
// extract zip
main.extractZipFiles();
// extract rar
main.extractRarFiles();
}
}

通过java实现解压zip,rar的代码的更多相关文章

  1. Java压缩/解压.zip、.tar.gz、.tar.bz2(支持中文)

    本文介绍Java压缩/解压.zip..tar.gz..tar.bz2的方式. 对于zip文件:使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile,通过设置 ...

  2. java实现解压zip文件,(亲测可用)!!!!!!

    项目结构: Util.java内容: package com.cfets.demo; import java.io.File; import java.io.FileOutputStream; imp ...

  3. JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包

    package zip; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStrea ...

  4. java 提取(解压)zip文件中特定后缀的文件并保存到指定目录

    内容简介 本文主要介绍使用ZipFile来提取zip压缩文件中特定后缀(如:png,jpg)的文件并保存到指定目录下. 导入包:import java.util.zip.ZipFile; 如需添加对r ...

  5. 原生java 压缩解压zip文件

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...

  6. Java动态解压zip压缩包

    import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; impo ...

  7. java无需解压zip压缩包直接读取包内的文件名(含中文)

    java自带了java.util.zip工具可以实现在不解压zip压缩包的情况下读取包内文件的文件名:(注:只能是ZIP格式的,rar我试了不行)代码如下: public static String ...

  8. java使用解压zip文件,文件名乱码解决方案

    File outFileDir = new File(outDir);if (!outFileDir.exists()) { boolean isMakDir = outFileDir.mkdirs( ...

  9. java批量解压文件夹下的所有压缩文件(.rar、.zip、.gz、.tar.gz)

    // java批量解压文件夹下的所有压缩文件(.rar..zip..gz..tar.gz) 新建工具类: package com.mobile.utils; import com.github.jun ...

随机推荐

  1. Safari 中加载 Flash 使用overflow失效的bug

    在Flash标签中加入参数:wmode=transparent

  2. div height 自适应高度 占满剩余高度的方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Django——photo

    要点: models 图片类型文件要使用models.ImageField(upload='文件夹名') 普通文件使用FileField 时间类型使用DatetimeField(auto_now_ad ...

  4. ae:org.apache.shiro.authc.AuthenticationException: Authentication token of type [class org.apache.shiro.authc.UsernamePasswordToken] could not be authenticated by any configured realms. Please ensure

    看问题是说要保证有一个realm正确,但是之前运行就一直成功,后来就不行了.有可能是:以为自己输入了正确的用户名和密码,结果是因为用户名前面添加了空格导致了这个错误发生. 实质问题是:你输入的用户名和 ...

  5. Flask jinja2

    {{ }} #引用 执行 非逻辑代码 {% %} #逻辑代码 引用变量 @app.template_global() # 全局函数 Markup # 安全标签字符串儿 {% macro func() ...

  6. css中px,em,rem,rpx的区别

    今天看到一个面试题为 px,em的区别,为了更好地让读者区分css的长度单位,我总结下css中常用的长度单位: px,em,rem,rpx 像素px是我们在定义CSS中经常用到的尺寸大小单位,而em在 ...

  7. helm-chart-2-chart结构和简单模板

    1, chart 结构介绍 我们创建一个chart 并查看其结构 右侧注释为其文件的的解释 $ helm create mychart $ cd mychart/ $ tree ├── charts ...

  8. keystone 安装随笔

    keystone 代码库 git clone https://git.openstack.org/openstack/keystone.git cd keystone keystone配置文件 etc ...

  9. tfs2015 生成与发布 配置

    先来看一张微软官方的自动生成与发布架构图,以便了解很多概念间的关系 1.安装好TFS2015(可以参考TFS2010的安装过程,尤其是账号权限相关),我自己是从TFS2010一路升级上来的(TFS20 ...

  10. .NET Core中使用IHostedService结合队列执行定时任务

    最近遇到了这样的场景:每隔一段时间,需要在后台使用队列对一批数据进行业务处理. Quartz.NET是一种选择,在 .NET Core中,可以使用IHostedService执行后台定时任务.在本篇中 ...