我是代码派
public class ZipUtils {
/**
* 解压文件
* @param zipPath
* @param toPath
* @throws IOException
*/
public static void unCompress(String zipPath, String toPath) throws IOException {
if(!toPath.endsWith("\\"))toPath += "\\";
File destFile = new File(toPath);
if(!destFile.exists())destFile.mkdirs();
File zipfile = new File(zipPath);
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipfile));
ZipEntry entry = null;
while ((entry = zis.getNextEntry()) != null) {
if (entry.isDirectory()) {
File file = new File(toPath + entry.getName() + "\\");
file.mkdirs();
} else {
File file = new File(toPath + entry.getName());
if(!file.getParentFile().exists())file.getParentFile().mkdirs();
FileOutputStream fos = new FileOutputStream(file);
byte buf[] = new byte[1024];
int len = -1;
while ((len = zis.read(buf, 0, 1024)) != -1) {
fos.write(buf, 0, len);
}
fos.close();
}
}
zis.close();
}
public static byte[] getData(String zipPath, String path) throws IOException{
File zipfile = new File(zipPath);
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipfile));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipEntry entry = null;
while ((entry = zis.getNextEntry()) != null) {
if(entry.getName().equals(path)){
byte buf[] = new byte[1024];
int len = -1;
while ((len = zis.read(buf, 0, 1024)) != -1) {
baos.write(buf, 0, len);
}
break;
}
}
baos.close();
zis.close();
return baos.toByteArray();
}
/**
* 压缩单个文件
* @param file
*/
public static void compress(File file) {
try {
String fileName = file.getName();
if(fileName.indexOf(".") != -1)
fileName = fileName.substring(0, fileName.indexOf("."));
FileOutputStream f = new FileOutputStream(file.getParent() + "/" + fileName+ ".zip");
CheckedOutputStream cs = new CheckedOutputStream(f, new Adler32());
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
InputStream in = new FileInputStream(file);
out.putNextEntry(new ZipEntry(file.getName()));
int len = -1;
byte buf[] = new byte[1024];
while((len = in.read(buf, 0, 1024)) != -1)
out.write(buf, 0, len);
out.closeEntry();
in.close();
out.close();
} catch (Exception e) {
System.err.println(e);
}
}
/**
* 压缩文件夹
* @param file
* @throws IOException
*/
public static void compressDir(File file) throws IOException{
FileOutputStream f = new FileOutputStream(file.getParent() + file.getName() + ".zip");
CheckedOutputStream cs = new CheckedOutputStream(f, new Adler32());
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
compressDir(file, out, file.getAbsolutePath());
out.flush();
out.close();
}
/**
* 压缩文件夹递归调用方法
* @param file
* @param out
* @throws IOException
*/
private static void compressDir(File srcFile, ZipOutputStream out, String destPath) throws IOException{
if(srcFile.isDirectory()){
File subfile[] = srcFile.listFiles();
for(int i=0; i< subfile.length; i++){
compressDir(subfile[i], out, destPath);
}
}else{
InputStream in = new FileInputStream(srcFile);
String name = srcFile.getAbsolutePath().replace(destPath, "");
if(name.startsWith("\\"))name = name.substring(1);
ZipEntry entry = new ZipEntry(name);
entry.setSize(srcFile.length());
entry.setTime(srcFile.lastModified());
out.putNextEntry(entry);
int len = -1;
byte buf[] = new byte[1024];
while((len = in.read(buf, 0, 1024)) != -1)
out.write(buf, 0, len);
out.closeEntry();
in.close();
}
}
public static void main(String[] args) throws Exception {
ZipUtils.compress(new File("d:/aa/a01.jpg"));
// try {
// byte buf[] = ZipDemo.getData("d:/aa.zip", "aa.txt");
// System.out.println(buf.length);
// ZipDemo.unCompress("d:/aa.zip", "d:/kk/");
// } catch (IOException e) {
// e.printStackTrace();
// }
}
分享到:
相关推荐
压缩解压代码示例,可以方便移植到自己程序中,欢迎使用。
RLE算法压缩解压源代码文件
zip压缩解压的源代码zip压缩解压的源代码
运用哈夫曼编码压缩解压文件源代码,代码有详细的注释,很好的压缩解压的源代码
zip 文件压缩 解压源码,c++版。已测试,100%可用! // XUnzip.cpp Version 1.3
代码实现解压,压缩linux下的GZ格式文件
zip 格式文件 压缩 解压缩 源代码 .h文件和 .cpp文件 内包含VC6.0 VS2008 测试程序 如何调用接口的测试程序
好用的文件压缩与解压代码
实验目的:理解哈弗曼信源编码算法,并能应用于文件压缩中。 实验内容:写出程序,利用哈弗曼编码实现对文件的压缩,并能解压文件。 实验步骤: 1、压缩 (1) 统计原始文件中各字节出现的概率(次数); (2) 采用...
提示:只需修改MyZip程序,将原先读取要加入的文件的内容后直接写到压缩文件中的代码,修改为先将要加入的文件压缩为一个临时文件后,再将压缩后的文件合并到原压缩文件中。压缩要加入的文件时,可调用LZARI类的...
将文件CodeFile以紧凑格式显示在终端上,每行50个代码。同时将此字符形式的编码文件写入文件CodePrin中。 (5) T:印哈夫曼树(Tree printing)。将已在内存中的哈夫曼树以直观的方式(比如树)显示在终端上,同时将...
java程序,压缩与解压rar文件. java程序,压缩与解压rar文件.
数据结构课程设计用哈夫曼编码实现文件压缩: 一、实验题目: 用哈夫曼编码实现文件压缩 二、实验目的: 1、了解文件的概念。 2、掌握线性链表的插入、删除等算法。 3、掌握Huffman树的概念及构造方法。 4、掌握...
java操作压缩文件和解压文件实例代码(经测试),把jar文件导入到项目里。源代码根据注释修改下路径就能用了
4 译码:对compress txt中的压缩字符进行解压缩 把解压的答案输出到文件decompress txt文件中 5 比较decompress txt和input txt中的字符是否完全相同 并计算压缩率 压缩率 compress txt文件大小 input txt的文件大小...
利用哈夫曼编码进行对已有文件进行重新编码可以大大提高减小文件大小,减少...但是,这要求在首先对一个现有文件进行编码行成新的文件,也就是压缩。在文件使用时,再对压缩文件进行解压缩,也就是译码,复原原有文件
文本压缩与解压源代码
通过赫夫曼编码实现的txt文件的压缩和解压的源代码,用xcode编的,就把源文件压缩传上来吧
cab文件压缩、解压程序源代码。是用C语言写的源代码。cab文件压缩、解压程序源代码。是用C语言写的源代码。
如果服务器上安装了RAR程序,那么asp.net可以调用RAR实现文件压缩与解压缩。 不过要注意的是,由于Web程序不能直接调用客户端的程序(除非用ActiveX,ActiveX几乎被废弃),所以如果要想实现让用户把本地文件用网页...