当前位置:网站首页>ZipOutputStream使用

ZipOutputStream使用

2022-06-10 11:57:00 wfsm

在这里插入图片描述
java提供的,类似于压缩软件的功能,可以解压缩文件
引用:https://www.jianshu.com/p/e9d01b3e82fc
https://blog.csdn.net/u010366748/article/details/78615004

public class Demo03 {
    

    public static void compress(File f, String basedir, ZipOutputStream zos) throws IOException {
    
        File[] fs = f.listFiles();
        BufferedInputStream bis = null;
        byte[] bytes = new byte[1024 * 10];
        FileInputStream fis = null;

        try {
    
            for (int i = 0; i < fs.length; i++) {
    
                String fName = fs[i].getName();
                System.out.println("压缩" + basedir + fName);

                if (fs[i].isFile()){
    
                    ZipEntry zipEntry = new ZipEntry(basedir + fName);
                    zos.putNextEntry(zipEntry);

                     fis = new FileInputStream(fs[i]);
                    bis = new BufferedInputStream(fis, 1024 * 10);
                    int read =0;
                    while ((read = bis.read(bytes,0,1024*10)) != -1){
    
                        zos.write(bytes,0,read);
                    }
                }else if(fs[i].isDirectory()){
    
                    compress(fs[i],basedir+fName+"/",zos);
                }

            }
        } catch (IOException e) {
    
            e.printStackTrace();
        } finally {
    
            if (bis != null){
    
                bis.close();
            }
            if (fis != null){
    
                fis.close();
            }
        }
    }

    public static void main(String[] args) throws IOException {
    
        ZipOutputStream zos = null;
        try {
    
            String sourceFilePath = "E:\\test";

            File sourceDir = new File(sourceFilePath);
            File zipFile = new File(sourceFilePath+".zip");
             zos = new ZipOutputStream(new FileOutputStream(zipFile));
            String basedir = "test/";
            compress(sourceDir,basedir,zos);
        } catch (IOException e) {
    
            e.printStackTrace();
        } finally {
    
            zos.close();
        }
    }
}
public class Client {
    
    private static void compressFileToZip(String filePath,String zipFilePath) throws IOException {
    
        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFilePath))) {
    
            doCompress("",filePath,zos);

            //
            zos.finish();
        }
    }

    private static void doCompress(String parentFilePath,String filePath,ZipOutputStream zos){
    
        // 根据 path 创建 file
        File sourceFile = new File(filePath);
        if (!sourceFile.exists()){
    
            return;
        }
        // 拼接路径
        String zipEntryName = parentFilePath+"/"+sourceFile.getName();

        if (parentFilePath.isEmpty()){
    
            zipEntryName = sourceFile.getName();
        }

        if (sourceFile.isDirectory()){
    
            File[] childrenFiles = sourceFile.listFiles();
            if (Objects.isNull(childrenFiles)){
    
                return;
            }
            for (File childFile : childrenFiles) {
    
                // 传入绝对路径
                doCompress(zipEntryName,childFile.getAbsolutePath(),zos);

            }
        }else{
    
            byte[] buffer = new byte[1024];
            try(BufferedInputStream input = new BufferedInputStream(new FileInputStream(sourceFile))){
    
                zos.putNextEntry(new ZipEntry(zipEntryName));
                int len;
                while ((len = input.read(buffer)) != -1){
    
                    zos.write(buffer,0,len);
                }
            }catch (Exception e){
    
                e.printStackTrace();
            }
        }

    }


    public static void main(String[] args) throws IOException {
    
// compressFileToZip("e://test","e://test1.zip");
        decompressFromZip("e://test1.zip","e:/hehe");
    }


    private static void decompressFromZip(String zipFilePath,String destFilePath) throws IOException {
    
        File file = new File(zipFilePath);
        ZipFile zipFile = new ZipFile(file);
        try (ZipInputStream zis = new ZipInputStream(new FileInputStream(file))) {
    
            ZipEntry zipEntry = null;
            while ((zipEntry = zis.getNextEntry()) != null){
    
                if (zipEntry.getName().equals(file.getName())){
    
                    continue;
                }
                String fileName = destFilePath + "/"+zipEntry.getName();
                File entryFile = new File(fileName);
                if (zipEntry.isDirectory()){
    
                    // 是文件夹
                    entryFile.mkdir();
                }else {
    
                    if (!entryFile.getParentFile().exists()){
    
                        // 父文件不存在,,必須保證父文件存在
                        entryFile.getParentFile().mkdirs();
                    }
                    entryFile.createNewFile();
                }


                try (
                        InputStream input = zipFile.getInputStream(zipEntry);
                        FileOutputStream output = new FileOutputStream(entryFile)) {
    

                    byte[] bytes = new byte[1024];
                    while (true){
    
                        int len = input.read(bytes);
                        if (len == -1){
    
                            break;
                        }
                        output.write(bytes,0,len);
                    }
                }


            }
        }

    }

}
原网站

版权声明
本文为[wfsm]所创,转载请带上原文链接,感谢
https://waterkid.blog.csdn.net/article/details/125183672