JAVA 将文件压缩为zip文件

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

 public static void main(String[]args) {  
        String sourceDir="C:\\Users\\Administrator\\Desktop\\新建文件夹\\标准符合测试系统开源软件.xls";

        String zipFile="C:\\Users\\Administrator\\Desktop\\新建文件夹\\a.zip";
            OutputStream os;  

            try {  

                os = new FileOutputStream(zipFile);  

                BufferedOutputStream bos = new BufferedOutputStream(os);  

                ZipOutputStream zos = new ZipOutputStream(bos);  

                File file = new File(sourceDir);  

                String basePath = null;  

                if (file.isDirectory()) {  

                    basePath = file.getPath();  

                } else {//直接压缩单个文件时,取父目录  

                    basePath = file.getParent();  

                }  

                zipFile(file, basePath, zos);  

                zos.closeEntry();  

                zos.close();  

            } catch (Exception e) {  

                e.printStackTrace();  

            }  

        }  

        /** 
         * 功能:执行文件压缩成zip文件 
         * @param source 
         * @param basePath  待压缩文件根目录 
         * @param zos 
         */  

        private static void zipFile(File source, String basePath,  

        ZipOutputStream zos) {  

            File[] files = new File[0];  

            if (source.isDirectory()) {  

                files = source.listFiles();  

            } else {  

                files = new File[1];  

                files[0] = source;  

            }  

            String pathName;//存相对路径(相对于待压缩的根目录)  

            byte[] buf = new byte[1024];  

            int length = 0;  

            try {  

                for (File file : files) {  

                    if (file.isDirectory()) {  

                        pathName = file.getPath().substring(basePath.length() + 1)  

                        + "/";  

                        zos.putNextEntry(new ZipEntry(pathName));  

                        zipFile(file, basePath, zos);  

                    } else {  

                        pathName = file.getPath().substring(basePath.length() + 1);  

                        InputStream is = new FileInputStream(file);  

                        BufferedInputStream bis = new BufferedInputStream(is);  

                        zos.putNextEntry(new ZipEntry(pathName));  

                        while ((length = bis.read(buf)) > 0) {  

                            zos.write(buf, 0, length);  

                        }  

                        is.close();  

                    }  

                }  

            } catch (Exception e) {  

                e.printStackTrace();  

            }  

        }  

猜你喜欢

转载自blog.csdn.net/lk7688535/article/details/78013481