java压缩解压缩完整例子(仅支持lzh格式)
来源:互联网 发布:七层网络 ip属于第几层 编辑:程序博客网 时间:2024/05/18 00:29
把下面文件放入同一目录下,运行使用 Run As Java Application 即可
//文件DirectoryZip.java, 压缩文件例子
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import jp.gr.java_conf.dangan.util.lha.LhaHeader;
import jp.gr.java_conf.dangan.util.lha.LhaOutputStream;
/** *//**
* @author tyrone
*
*/
public class DirectoryZip ...{
/** *//**
*@param inputFileName, file or directory waiting for zipping ,outputFileName output file name
*
*/
public void zip(String inputFileName,String outputFileName) throws Exception ...{
LhaOutputStream out = new LhaOutputStream(new FileOutputStream(outputFileName));
zip(out, new File(inputFileName), "");
System.out.println("zip done");
out.close();
}
private void zip(LhaOutputStream out, File f, String base) throws Exception ...{
if (f.isDirectory()) ...{
File[] fl = f.listFiles();
if (System.getProperty("os.name").startsWith("Windows"))...{
out.putNextEntry(new LhaHeader(base + "/"));
base = base.length() == 0 ? "" : base + "/";
}
else...{
out.putNextEntry(new LhaHeader(base + "/"));
base = base.length() == 0 ? "" : base + "/";
}
for (int i = 0; i < fl.length; i++) ...{
zip(out, fl[i], base + fl[i].getName());
}
}
else ...{
out.putNextEntry(new LhaHeader(base));
//***乱????理
//BufferedReader in=new BufferedReader(
//new InputStreamReader(new FileInputStream(f),"ISO8859_1"));
FileInputStream in = new FileInputStream(f);
int b;
System.out.println(base);
while ( (b = in.read()) != -1) ...{
out.write(b);
}
in.close();
}
}
public static void main(String[] args)...{
DirectoryZip m_zip=new DirectoryZip();
try...{
m_zip.zip("E:/temp","E:/www.lzh");
}catch(Exception ex)...{
ex.printStackTrace();
}
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import jp.gr.java_conf.dangan.util.lha.LhaHeader;
import jp.gr.java_conf.dangan.util.lha.LhaOutputStream;
/** *//**
* @author tyrone
*
*/
public class DirectoryZip ...{
/** *//**
*@param inputFileName, file or directory waiting for zipping ,outputFileName output file name
*
*/
public void zip(String inputFileName,String outputFileName) throws Exception ...{
LhaOutputStream out = new LhaOutputStream(new FileOutputStream(outputFileName));
zip(out, new File(inputFileName), "");
System.out.println("zip done");
out.close();
}
private void zip(LhaOutputStream out, File f, String base) throws Exception ...{
if (f.isDirectory()) ...{
File[] fl = f.listFiles();
if (System.getProperty("os.name").startsWith("Windows"))...{
out.putNextEntry(new LhaHeader(base + "/"));
base = base.length() == 0 ? "" : base + "/";
}
else...{
out.putNextEntry(new LhaHeader(base + "/"));
base = base.length() == 0 ? "" : base + "/";
}
for (int i = 0; i < fl.length; i++) ...{
zip(out, fl[i], base + fl[i].getName());
}
}
else ...{
out.putNextEntry(new LhaHeader(base));
//***乱????理
//BufferedReader in=new BufferedReader(
//new InputStreamReader(new FileInputStream(f),"ISO8859_1"));
FileInputStream in = new FileInputStream(f);
int b;
System.out.println(base);
while ( (b = in.read()) != -1) ...{
out.write(b);
}
in.close();
}
}
public static void main(String[] args)...{
DirectoryZip m_zip=new DirectoryZip();
try...{
m_zip.zip("E:/temp","E:/www.lzh");
}catch(Exception ex)...{
ex.printStackTrace();
}
}
}
//文件Unzip.java, 解压lzh文件的例子
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import jp.gr.java_conf.dangan.util.lha.LhaHeader;
import jp.gr.java_conf.dangan.util.lha.LhaInputStream;
/** *//** *//** *//**
* 使用java核心??打包、解包zip文件,不足之?在于??中文名的文件?,在??包内中文字符是乱?,
* 在windows下解?后??正常?示中文,而其他系?下?不能正常?原;
*/
public class Unzip ...{
/** *//** *//** *//**
* 定?解??zip文件的方法
* @param zipFileName
* @param outputDirectory
*/
public void unzip(String zipFileName, String outputDirectory) ...{
try ...{
// BufferedReader in=new BufferedReader(
// new InputStreamReader(new ZipInputStream(new FileInputStream(zipFileName)),"ISO8859_1"));
///ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
LhaInputStream in = new LhaInputStream(new FileInputStream(zipFileName));
//?取ZipInputStream中的ZipEntry条目,一个zip文件中可能包含多个ZipEntry,
//当getNextEntry方法的返回??null,?代表ZipInputStream中没有下一个ZipEntry,
//?入流?取完成;
LhaHeader z = in.getNextEntry();
while (z != null) ...{
//System.out.println("unziping " + z.getName());
//?建以zip包文件名?目?名的根目?
File f = new File(outputDirectory);
f.mkdir();
if (z.getPath().endsWith("/")) ...{
String path = z.getPath();
path = path.substring(0, path.length() - 1);
System.out.println("path " + path);
f = new File(outputDirectory + path);
f.mkdir();
System.out.println("mkdir " + outputDirectory + File.separator + path);
}
else ...{
if(z.getPath().lastIndexOf('/')>0)...{
f = new File(outputDirectory + z.getPath().substring(z.getPath().lastIndexOf('/')));
} else ...{
f = new File(outputDirectory + z.getPath());
}
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
int b;
while ((b = in.read()) != -1) ...{
out.write(b);
}
out.close();
}
//?取下一个ZipEntry
z = in.getNextEntry();
}
in.close();
}
catch (Exception e) ...{
// TODO 自?生成 catch ?
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception...{
Unzip t = new Unzip();
t.unzip("e:/www.lzh", "e:/bbb");
//解?C?下的a.zip文件放到C?下的b文件?里
//一定要注意a.zip文件要?java程序??出来的文件,?个程序才可以解?
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import jp.gr.java_conf.dangan.util.lha.LhaHeader;
import jp.gr.java_conf.dangan.util.lha.LhaInputStream;
/** *//** *//** *//**
* 使用java核心??打包、解包zip文件,不足之?在于??中文名的文件?,在??包内中文字符是乱?,
* 在windows下解?后??正常?示中文,而其他系?下?不能正常?原;
*/
public class Unzip ...{
/** *//** *//** *//**
* 定?解??zip文件的方法
* @param zipFileName
* @param outputDirectory
*/
public void unzip(String zipFileName, String outputDirectory) ...{
try ...{
// BufferedReader in=new BufferedReader(
// new InputStreamReader(new ZipInputStream(new FileInputStream(zipFileName)),"ISO8859_1"));
///ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
LhaInputStream in = new LhaInputStream(new FileInputStream(zipFileName));
//?取ZipInputStream中的ZipEntry条目,一个zip文件中可能包含多个ZipEntry,
//当getNextEntry方法的返回??null,?代表ZipInputStream中没有下一个ZipEntry,
//?入流?取完成;
LhaHeader z = in.getNextEntry();
while (z != null) ...{
//System.out.println("unziping " + z.getName());
//?建以zip包文件名?目?名的根目?
File f = new File(outputDirectory);
f.mkdir();
if (z.getPath().endsWith("/")) ...{
String path = z.getPath();
path = path.substring(0, path.length() - 1);
System.out.println("path " + path);
f = new File(outputDirectory + path);
f.mkdir();
System.out.println("mkdir " + outputDirectory + File.separator + path);
}
else ...{
if(z.getPath().lastIndexOf('/')>0)...{
f = new File(outputDirectory + z.getPath().substring(z.getPath().lastIndexOf('/')));
} else ...{
f = new File(outputDirectory + z.getPath());
}
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
int b;
while ((b = in.read()) != -1) ...{
out.write(b);
}
out.close();
}
//?取下一个ZipEntry
z = in.getNextEntry();
}
in.close();
}
catch (Exception e) ...{
// TODO 自?生成 catch ?
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception...{
Unzip t = new Unzip();
t.unzip("e:/www.lzh", "e:/bbb");
//解?C?下的a.zip文件放到C?下的b文件?里
//一定要注意a.zip文件要?java程序??出来的文件,?个程序才可以解?
}
}
注意:本程序引用的包,不是java自带的,需要下载导入。 以下是下载地址:
http://packages.debian.org/source/etch/all/libjlha-java
或者到 http://hiyu2218.download.csdn.net/ 在搜索中输入 libjlha-java 去搜索,偶已经上传该资源了。
- java压缩解压缩完整例子(仅支持lzh格式)
- lzh文件解压缩
- java实现单个或多个文件的压缩、解压缩 支持zip、rar等格式
- Java使用ZIP格式压缩与解压缩
- Linux 压缩解压缩例子
- 压缩解压缩文件(zip格式)
- java压缩解压缩ZipFile完整源码-可以直接拿来用
- zip格式压缩和解压缩(支持中文和文件嵌套解压缩)
- 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- 3级联动下拉列表-数据库版
- CodeReview到底干点啥?
- 关于公司部门的SQL语句,两问
- DOS下的Tree命令
- ≡ Windows XP + Apache 2.2.4 + PHP 5.2 + MySQL 5.0.18 +eclipse3.2+wampp-2.2.zip+HardCore环境配置说明 ≡
- java压缩解压缩完整例子(仅支持lzh格式)
- 建模工具Rose的学习(1)
- 关于OEM
- [UML]创建图所应遵循的策略
- ASP.NET 3.5's ListView and DataPager—Part3:用ListView控件排序数据
- 如何正确的使用CFile::Open中的参数?
- VB API处理INI文件函数
- [UML]绘制图应遵循的策略
- 巧合的图片