文件压缩和解压zip
来源:互联网 发布:java中文怎么发音 编辑:程序博客网 时间:2024/05/01 06:24
/**文件压缩和解压zip
* @author jiangbing
*
*/
public class Unzip {
public static void zip(String src, String dest) throws IOException {
// 提供了一个数据项压缩成一个ZIP归档输出流
ZipOutputStream out = null;
try {
File outFile = new File(dest);// 源文件或者目录
File fileOrDirectory = new File(src);// 压缩文件路径
out = new ZipOutputStream(new FileOutputStream(outFile));
// 如果此文件是一个文件,否则为false。
if (fileOrDirectory.isFile()) {
zipFileOrDirectory(out, fileOrDirectory, "");
} else {
// 返回一个文件或空阵列。
File[] entries = fileOrDirectory.listFiles();
for (int i = 0; i < entries.length; i++) {
// 递归压缩,更新curPaths
zipFileOrDirectory(out, entries[i], "");
}
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
// 关闭输出流
if (out != null) {
try {
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
private static void zipFileOrDirectory(ZipOutputStream out,
File fileOrDirectory, String curPath) throws IOException {
// 从文件中读取字节的输入流
FileInputStream in = null;
try {
// 如果此文件是一个目录,否则返回false。
if (!fileOrDirectory.isDirectory()) {
// 压缩文件
byte[] buffer = new byte[4096];
int bytes_read;
in = new FileInputStream(fileOrDirectory);
// 实例代表一个条目内的ZIP归档
ZipEntry entry = new ZipEntry(curPath
+ fileOrDirectory.getName());
// 条目的信息写入底层流
out.putNextEntry(entry);
while ((bytes_read = in.read(buffer)) != -1) {
out.write(buffer, 0, bytes_read);
}
out.closeEntry();
} else {
// 压缩目录
File[] entries = fileOrDirectory.listFiles();
for (int i = 0; i < entries.length; i++) {
// 递归压缩,更新curPaths
zipFileOrDirectory(out, entries[i], curPath
+ fileOrDirectory.getName() + "/");
}
}
} catch (IOException ex) {
ex.printStackTrace();
// throw ex;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
@SuppressWarnings("unchecked")
public static void unzip(String zipFileName, String outputDirectory,Context context)
throws IOException {
ZipFile zipFile = null;
try {
zipFile = new ZipFile(zipFileName);
Enumeration e = zipFile.entries();
ZipEntry zipEntry = null;
File dest = new File(outputDirectory);
dest.mkdirs();
while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement();
String entryName = zipEntry.getName();
InputStream in = null;
FileOutputStream out = null;
try {
if (zipEntry.isDirectory()) {
String name = zipEntry.getName();
name = name.substring(0, name.length() - 1);
File f = new File(outputDirectory + File.separator
+ name);
f.mkdirs();
//创建出来libs文件
} else {
int index = entryName.lastIndexOf("\\");
if (index != -1) {
File df = new File(outputDirectory + File.separator
+ entryName.substring(0, index));
df.mkdirs();
}
index = entryName.lastIndexOf("/");
if (index != -1) {
File df = new File(outputDirectory + File.separator
+ entryName.substring(0, index));
df.mkdirs();
}
File f = new File(outputDirectory + File.separator
+ zipEntry.getName());
// f.createNewFile();
Runtime.getRuntime().exec("chmod 777 " + f);
in = zipFile.getInputStream(zipEntry);
out = new FileOutputStream(f);
int c;
byte[] by = new byte[1024];
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
out.flush();
// //解压结束后删除原文件
// File file = new File(zipFileName);
// if(file.exists()){
// file.delete();
// }
}
} catch (IOException ex) {
ex.printStackTrace();
// fileCallback.failed();
SharedPreferences sharedPreferences = context.getSharedPreferences("downLoadZip", Context.MODE_PRIVATE);
sharedPreferences.edit().putBoolean("downLoadZip", false).commit();
throw new IOException("解压失败:" + ex.toString());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException ex) {
}
}
}
}
} catch (IOException ex) {
ex.printStackTrace();
SharedPreferences sharedPreferences = context.getSharedPreferences("downLoadZip", Context.MODE_PRIVATE);
sharedPreferences.edit().putBoolean("downLoadZip", false).commit();
throw new IOException("解压失败:" + ex.toString());
} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException ex) {
}
}
}
}
}
* @author jiangbing
*
*/
public class Unzip {
public static void zip(String src, String dest) throws IOException {
// 提供了一个数据项压缩成一个ZIP归档输出流
ZipOutputStream out = null;
try {
File outFile = new File(dest);// 源文件或者目录
File fileOrDirectory = new File(src);// 压缩文件路径
out = new ZipOutputStream(new FileOutputStream(outFile));
// 如果此文件是一个文件,否则为false。
if (fileOrDirectory.isFile()) {
zipFileOrDirectory(out, fileOrDirectory, "");
} else {
// 返回一个文件或空阵列。
File[] entries = fileOrDirectory.listFiles();
for (int i = 0; i < entries.length; i++) {
// 递归压缩,更新curPaths
zipFileOrDirectory(out, entries[i], "");
}
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
// 关闭输出流
if (out != null) {
try {
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
private static void zipFileOrDirectory(ZipOutputStream out,
File fileOrDirectory, String curPath) throws IOException {
// 从文件中读取字节的输入流
FileInputStream in = null;
try {
// 如果此文件是一个目录,否则返回false。
if (!fileOrDirectory.isDirectory()) {
// 压缩文件
byte[] buffer = new byte[4096];
int bytes_read;
in = new FileInputStream(fileOrDirectory);
// 实例代表一个条目内的ZIP归档
ZipEntry entry = new ZipEntry(curPath
+ fileOrDirectory.getName());
// 条目的信息写入底层流
out.putNextEntry(entry);
while ((bytes_read = in.read(buffer)) != -1) {
out.write(buffer, 0, bytes_read);
}
out.closeEntry();
} else {
// 压缩目录
File[] entries = fileOrDirectory.listFiles();
for (int i = 0; i < entries.length; i++) {
// 递归压缩,更新curPaths
zipFileOrDirectory(out, entries[i], curPath
+ fileOrDirectory.getName() + "/");
}
}
} catch (IOException ex) {
ex.printStackTrace();
// throw ex;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
@SuppressWarnings("unchecked")
public static void unzip(String zipFileName, String outputDirectory,Context context)
throws IOException {
ZipFile zipFile = null;
try {
zipFile = new ZipFile(zipFileName);
Enumeration e = zipFile.entries();
ZipEntry zipEntry = null;
File dest = new File(outputDirectory);
dest.mkdirs();
while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement();
String entryName = zipEntry.getName();
InputStream in = null;
FileOutputStream out = null;
try {
if (zipEntry.isDirectory()) {
String name = zipEntry.getName();
name = name.substring(0, name.length() - 1);
File f = new File(outputDirectory + File.separator
+ name);
f.mkdirs();
//创建出来libs文件
} else {
int index = entryName.lastIndexOf("\\");
if (index != -1) {
File df = new File(outputDirectory + File.separator
+ entryName.substring(0, index));
df.mkdirs();
}
index = entryName.lastIndexOf("/");
if (index != -1) {
File df = new File(outputDirectory + File.separator
+ entryName.substring(0, index));
df.mkdirs();
}
File f = new File(outputDirectory + File.separator
+ zipEntry.getName());
// f.createNewFile();
Runtime.getRuntime().exec("chmod 777 " + f);
in = zipFile.getInputStream(zipEntry);
out = new FileOutputStream(f);
int c;
byte[] by = new byte[1024];
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
out.flush();
// //解压结束后删除原文件
// File file = new File(zipFileName);
// if(file.exists()){
// file.delete();
// }
}
} catch (IOException ex) {
ex.printStackTrace();
// fileCallback.failed();
SharedPreferences sharedPreferences = context.getSharedPreferences("downLoadZip", Context.MODE_PRIVATE);
sharedPreferences.edit().putBoolean("downLoadZip", false).commit();
throw new IOException("解压失败:" + ex.toString());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException ex) {
}
}
}
}
} catch (IOException ex) {
ex.printStackTrace();
SharedPreferences sharedPreferences = context.getSharedPreferences("downLoadZip", Context.MODE_PRIVATE);
sharedPreferences.edit().putBoolean("downLoadZip", false).commit();
throw new IOException("解压失败:" + ex.toString());
} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException ex) {
}
}
}
}
}
0 0
- 文件压缩和解压zip
- 压缩和解压zip文件
- .zip文件解压、压缩
- Java 解压和压缩密码zip文件
- Ionic.Zip.dll文件压缩和解压
- Ionic.Zip.dll文件压缩和解压
- linux 安装zip和压缩解压文件
- zip文件的压缩和解压
- java解压和压缩zip文件
- 压缩zip文件和解压zip格式的文件
- ZIP压缩和解压
- ZIP压缩和解压
- zip压缩和解压
- 写着玩的zip解压和压缩zip文件
- C#压缩解压zip 文件
- C#压缩解压zip 文件
- C#压缩解压zip 文件
- Android 压缩解压zip文件
- 第13周项目 1.4 删去后 重排
- 第十三周项目一——数组大折腾(2)
- 第13周项目4-数组的排序-(3)
- Spring的Quartz定时器同一时刻重复执行二次的问题解决
- 第13周项目3-成绩处理函数版
- 文件压缩和解压zip
- WinSIP使用教程 使用winsip性能测试
- 分糖果
- scrapy 下载gif图片
- 第13周项目5.1.4 统计每一个字母出现的次数
- apache 301重定向
- 第十三周上机项目四3选择排序
- android textview字儿数多于指定行数用省略号代替
- 基于堆实现的优先级队列:PriorityQueue 解决 Top K 问题