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. C++学习笔记52:查找

    //函数查找 template <class T> int seqSearch(const T list[], int n, const T &key) { ; i < n; ...

  2. ES6 迭代器和生成器

    设计为了更高效的数据处理,避免过多for循环嵌套(代码复杂度,跟踪多个循环变量) 1. 迭代器: 为迭代过程设计的接口 所有的迭代器对象都有next()方法,每次调用都返回一个结果对象,对象有两个属性 ...

  3. redis:hash哈希类型的操作

    1. hash哈希类型的操作 1.1. hset key field value 语法:hset key field value 作用:把key中field域的值设为value 注:如果没有field ...

  4. Java 集合总体框架介绍

    Java集合是java提供的工具包,包含了常用的数据结构:集合.链表.队列.栈.数组.映射等.Java集合工具包位置是java.util.*Java集合主要可以划分为4个部分:List列表.Set集合 ...

  5. lettcode21. Merge Two Sorted Lists

    lettcode21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The ne ...

  6. [Python设计模式] 第26章 千人千面,内在共享——享元模式

    github地址:https://github.com/cheesezh/python_design_patterns 背景 有6个客户想做产品展示网站,其中3个想做成天猫商城那样的"电商风 ...

  7. grid - 隐式网格

    当网格项目确认在显式网格之外时就会创建隐性网格,当没有足够的空间或者显式的网格轨道来设置网格项目,此时网格项目就会自动创建隐式网格. 隐式网格可以定义:grid-auto-rows.grid-auto ...

  8. emacs 集成astyle

    https://stackoverflow.com/questions/8115460/emacs-save-excursion-not-restoring-point https://github. ...

  9. MySQL技术内幕读书笔记(五)——索引与算法

    索引与算法 INNODB存储引擎索引概述 ​ INNODB存储引擎支持以下几种常见的索引: B+树索引 全文索引 哈希索引 ​ InnoDB存储引擎支持的哈希索引是自适应的.会根据表的情况自动添加 ​ ...

  10. 在新安装的Centos中安装python3.7 解决pip和yum问题

    首先要先安装依赖包: yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-deve ...