`

java代码实现文件打包

    博客分类:
  • java
阅读更多
网上找了一个文件打包的程序。感觉不错拿来用用^_^
package test;   
  
import java.io.BufferedInputStream;   
import java.io.BufferedOutputStream;   
import java.io.File;   
import java.io.FileInputStream;   
import java.io.FileOutputStream;   
import java.util.zip.ZipEntry;   
import java.util.zip.ZipOutputStream;   
  
public class Zip {   
  
    /**   
     * 压缩文件夹   
     * @param zipPath   生成的zip文件路径   
     * @param filePath  需要压缩的文件夹路径   
     * @throws Exception   
     */  
    public void zipFolder(String zipPath, String filePath) throws Exception {   
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipPath));   
        File f = new File(filePath);   
        zipFiles(out, f, "");   
        out.close();   
    }      
  
    /**   
     * 压缩单一个文件   
     * @param zipPath   生成的zip文件路径   
     * @param filePath  需要压缩的文件路径   
     * @throws Exception   
     */  
    public void zipFile(String zipPath, String filePath) throws Exception {   
        File f = new File(filePath);   
        FileInputStream fis = new FileInputStream(f);   
        BufferedInputStream bis = new BufferedInputStream(fis);   
        byte[] buf = new byte[1024];   
        int len;   
        FileOutputStream fos = new FileOutputStream(zipPath);   
        BufferedOutputStream bos = new BufferedOutputStream(fos);   
        ZipOutputStream zos = new ZipOutputStream(bos);//压缩包   
        ZipEntry ze = new ZipEntry(f.getName());//这是压缩包名里的文件名   
        zos.putNextEntry(ze);// 写入新的ZIP文件条目并将流定位到条目数据的开始处   
  
        while ((len = bis.read(buf)) != -1) {   
            zos.write(buf, 0, len);   
            zos.flush();   
        }   
        bis.close();   
        zos.close();   
  
    }   
       
    /**   
     * 递归调用,压缩文件夹和子文件夹的所有文件   
     * @param out   
     * @param f   
     * @param base   
     * @throws Exception   
     */  
    private void zipFiles(ZipOutputStream out, File f, String base) throws Exception {   
        if (f.isDirectory()) {   
            File[] fl = f.listFiles();   
            out.putNextEntry(new ZipEntry(base + "/"));   
            base = base.length() == 0 ? "" : base + "/";   
            for (int i = 0; i < fl.length; i++) {   
                zipFiles(out, fl[i], base + fl[i].getName());//递归压缩子文件夹   
            }   
        } else {   
            out.putNextEntry(new ZipEntry(base));   
            FileInputStream in = new FileInputStream(f);   
            int b;   
            //System.out.println(base);   
            while ((b = in.read()) != -1) {   
                out.write(b);   
            }   
            in.close();   
        }   
    }   
  
    /**   
     * @param args   
     */  
    public static void main(String[] args) {   
        System.out.println("Zip File Begin");   
  
        Zip zip = new Zip();   
        String zipPath = "D:\\test.zip";   
        String filePath = "D:\\test.txt";   
        try {   
            zip.zipFile(zipPath, filePath);//压缩文件   
            //zip.zipFolder(zipPath, filePath);//压缩文件夹   
        } catch (Exception ex) {   
            ex.printStackTrace();   
        }   
        System.out.println("Zip File Done");   
    }   
  
}  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics