Java 文件压缩和解压

Posted 瞌睡先生想睡觉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 文件压缩和解压相关的知识,希望对你有一定的参考价值。

package com.wkk.util;

import java.io.*;
import java.util.zip.*;

public class Util 

    /**
     * 压缩文件
     *
     * @param zipPath 文件压缩后生成的文件路径
     * @param f       需要压缩的文件
     * @throws IOException
     */
    public static void compressionFile(String zipPath, File f) throws IOException 
        ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipPath));
        File files[] = new File[]f;
        for (int i = 0; i < files.length; i++) 
            File file = files[i];
            if (file.isDirectory()) 
                isDirectory(zipOutputStream, file, file.getName());
             else if (file.isFile()) 
                isFile(zipOutputStream, file, "");
            
        
        zipOutputStream.flush();
        zipOutputStream.close();
    

    private static void isDirectory(ZipOutputStream zipOutputStream, File file, String pathName) throws IOException 
        File[] files = file.listFiles();
        if (files.length > 0) 
            for (int i = 0; i < files.length; i++) 
                File file1 = files[i];
                if (file1.isDirectory()) 
                    isDirectory(zipOutputStream, file1, pathName + File.separator + file1.getName());
                 else if (file1.isFile()) 
                    isFile(zipOutputStream, file1, pathName);
                
            
         else 
            String name = pathName + "/";
            ZipEntry zipEntry = new ZipEntry(name);
            zipOutputStream.putNextEntry(zipEntry);
            zipOutputStream.closeEntry();
        
    

    private static void isFile(ZipOutputStream zipOutputStream, File file, String pathName) throws IOException 
        ZipEntry zipEntry = null;
        if (pathName == null || pathName.equalsIgnoreCase("")) 
            zipEntry = new ZipEntry(file.getName());
         else 
            zipEntry = new ZipEntry(pathName + File.separator + file.getName());
        
        zipOutputStream.putNextEntry(zipEntry);
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] b = new byte[1024];
        int l = 0;
        while ((l = fileInputStream.read(b)) > 0) 
            zipOutputStream.write(b, 0, l);
        
        fileInputStream.close();
        zipOutputStream.closeEntry();
    


    /**
     * 解压文件 如果压缩文件中有文件夹将会发生异常
     *
     * @param zipFilePath 压缩文件路径
     * @param unpackPath  解压后文件存放路径
     * @throws IOException
     */
    public static void UnpackFile(String zipFilePath, String unpackPath) throws IOException 
        File file = new File(zipFilePath);
        ZipFile zipFile = new ZipFile(file);
        ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(file));
        ZipEntry zipEntry = null;
        while ((zipEntry = zipInputStream.getNextEntry()) != null) 
            String path = unpackPath + "\\\\" + zipEntry.getName();
            new File(path).mkdirs();
            if (zipEntry.isDirectory()) 
                continue;
            
            new File(path).delete();
            FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
            InputStream inputStream = zipFile.getInputStream(zipEntry);
            byte b[] = new byte[1024];
            int l = 0;
            while ((l = inputStream.read(b)) > 0) 
                fileOutputStream.write(b, 0, l);
            
            inputStream.close();
            fileOutputStream.flush();
            fileOutputStream.close();
        
        zipFile.close();
        zipInputStream.close();
    


欢迎交流,与君共勉

以上是关于Java 文件压缩和解压的主要内容,如果未能解决你的问题,请参考以下文章

Java实现文件压缩与解压

Java压缩文件以及解压文件

Java 文件压缩和解压

java程序压缩和解压zip文件

每日一例 | java实现文件压缩和解压

Swift 压缩和解压文件的框架 Zip