java压缩解压缩完整例子(仅支持lzh格式)
2008-01-25 11:07
316 查看
把下面文件放入同一目录下,运行使用 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();
}
}
}
//文件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程序??出来的文件,?个程序才可以解?
}
}
注意:本程序引用的包,不是java自带的,需要下载导入。 以下是下载地址:
http://packages.debian.org/source/etch/all/libjlha-java
或者到 http://hiyu2218.download.csdn.net/ 在搜索中输入 libjlha-java 去搜索,偶已经上传该资源了。
//文件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();
}
}
}
//文件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程序??出来的文件,?个程序才可以解?
}
}
注意:本程序引用的包,不是java自带的,需要下载导入。 以下是下载地址:
http://packages.debian.org/source/etch/all/libjlha-java
或者到 http://hiyu2218.download.csdn.net/ 在搜索中输入 libjlha-java 去搜索,偶已经上传该资源了。
相关文章推荐
- java实现单个或多个文件的压缩、解压缩 支持zip、rar等格式
- 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- 从 Java 支持压缩/解压缩开始,无法缺省支持中文文件名。
- lha命令_Linux lha 命令用法详解:压缩或解压缩lzh格式文件
- 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- 利用Java实现zip压缩解压缩
- python的tarfile模块实例 python把文件夹压缩成tar格式文件的例子
- 把内容写入指定目录指定文件的java文件工具类,支持日期格式目录名的生成