JAVA
来源:互联网 发布:淘宝美工常用软件 编辑:程序博客网 时间:2024/04/29 15:05
程序实现了ZIP压缩。共分为2部分 : 压缩(compression)与解压(decompression)
大致功能包括用了多态,递归等Java核心技术,可以对单个文件和任意级联文件夹进行压缩和解压。 需在代码中自定义源输入路径和目标输出路径。
- package com.han;
-
- import java.io.*;
- import java.util.zip.*;
-
-
-
-
-
-
-
-
-
-
-
-
- public class MyZipCompressing {
- private int k = 1;
-
- public MyZipCompressing() {
-
- }
-
-
-
-
- public static void main(String[] args) {
-
- MyZipCompressing book = new MyZipCompressing();
- try {
- book.zip("C:\\Users\\Gaowen\\Desktop\\ZipTestCompressing.zip",
- new File("C:\\Users\\Gaowen\\Documents\\Tencent Files"));
- } catch (Exception e) {
-
- e.printStackTrace();
- }
-
- }
-
- private void zip(String zipFileName, File inputFile) throws Exception {
- System.out.println("压缩中...");
- ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
- zipFileName));
- BufferedOutputStream bo = new BufferedOutputStream(out);
- zip(out, inputFile, inputFile.getName(), bo);
- bo.close();
- out.close();
- System.out.println("压缩完成");
- }
-
- private void zip(ZipOutputStream out, File f, String base,
- BufferedOutputStream bo) throws Exception {
- if (f.isDirectory()) {
- File[] fl = f.listFiles();
- if (fl.length == 0) {
- out.putNextEntry(new ZipEntry(base + "/"));
- System.out.println(base + "/");
- }
- for (int i = 0; i < fl.length; i++) {
- zip(out, fl[i], base + "/" + fl[i].getName(), bo);
- }
- System.out.println("第" + k + "次递归");
- k++;
- } else {
- out.putNextEntry(new ZipEntry(base));
- System.out.println(base);
- FileInputStream in = new FileInputStream(f);
- BufferedInputStream bi = new BufferedInputStream(in);
- int b;
- while ((b = bi.read()) != -1) {
- bo.write(b);
- }
- bi.close();
- in.close();
- }
- }
- }
- package com.han;
-
- import java.io.*;
- import java.util.zip.*;
-
-
-
-
-
-
-
-
-
-
-
- public class CopyOfMyzipDecompressing {
-
- public static void main(String[] args) {
-
- long startTime=System.currentTimeMillis();
- try {
- ZipInputStream Zin=new ZipInputStream(new FileInputStream(
- "C:\\Users\\HAN\\Desktop\\stock\\SpectreCompressed.zip"));
- BufferedInputStream Bin=new BufferedInputStream(Zin);
- String Parent="C:\\Users\\HAN\\Desktop";
- File Fout=null;
- ZipEntry entry;
- try {
- while((entry = Zin.getNextEntry())!=null && !entry.isDirectory()){
- Fout=new File(Parent,entry.getName());
- if(!Fout.exists()){
- (new File(Fout.getParent())).mkdirs();
- }
- FileOutputStream out=new FileOutputStream(Fout);
- BufferedOutputStream Bout=new BufferedOutputStream(out);
- int b;
- while((b=Bin.read())!=-1){
- Bout.write(b);
- }
- Bout.close();
- out.close();
- System.out.println(Fout+"解压成功");
- }
- Bin.close();
- Zin.close();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- } catch (FileNotFoundException e) {
-
- e.printStackTrace();
- }
- long endTime=System.currentTimeMillis();
- System.out.println("耗费时间: "+(endTime-startTime)+" ms");
- }
-
- }
更多资料:
http://www.cnblogs.com/java-class/p/5662934.html
http://blog.csdn.net/gaowen_han/article/details/7163737/
http://blog.csdn.net/stalwartwill/article/details/17373827
Java不同压缩算法的性能比较