您的位置:首页 > 编程语言 > Java开发

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 去搜索,偶已经上传该资源了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: