JAVA 代码处理 文件的压缩和解压

先上参考代码方法,复制粘贴过去即可用:

package _toolsUtils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;


public class FileAndByteUtils {
/**
* 获取文件字节流
* @param file
* @return
*/
public static byte[] readFromFile(File file) {
byte[] b = new byte[1024];
int alllen = 0;
FileInputStream fis = null;
BufferedInputStream bis = null;
byte[] bytes = new byte[(int) file.length()];
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
int len = -1;
while ((len = bis.read(b)) != -1) {
for (int i = 0; i < len; i++) {
bytes[alllen++] = b[i];
}
}
bis.close();
return bytes;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

    /**
     * 压缩文件字节流
     * @param data
     * @return
     */
    public static byte[] zip(byte[] data) {
        byte[] b = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ZipOutputStream zip = new ZipOutputStream(bos);
            ZipEntry entry = new ZipEntry("asfdff/zip");
            entry.setSize(data.length);
            zip.putNextEntry(entry);
            zip.write(data);
            zip.closeEntry();
            zip.close();
            b = bos.toByteArray();
            bos.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return b;
    }

    /**
     * 
     * @param data 压缩文件的字节流
     * @param baseDir 解压下的目录
     * @return
     */
    public static byte[] unZip(byte[] data, String baseDir) {
        byte[] b = null;
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(data);
            ZipInputStream zip = new ZipInputStream(bis);
            ZipEntry zipEntry = null; 
            while ((zipEntry = zip.getNextEntry()) != null) {
                byte[] buf = new byte[1024];
                int num = -1;
                File file=new File(baseDir + zipEntry.getName());
                if(!file.exists()) {
                if(!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
                }
                }else {
                file.delete();
                }
                file.createNewFile();
                FileOutputStream fos = new FileOutputStream(file);
                while ((num = zip.read(buf)) != -1) {
                    fos.write(buf, 0, num);
                    fos.flush();
                }
                fos.close();
            }
            zip.close();
            bis.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return b;
    }
  /**
   * 
   * @param fileBytes 文件字节流
   * @param filePaths 文件路径
   */
    public static void writeToFile(byte [] fileBytes,String filePaths){
        File file = new File(filePaths);
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(fileBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
    }

    /**
     * 
     * @param dir 解压路径
     * @param zipFile 压缩文件
     */
    public static void unzipZipFile(String dir, File zipFile) {
    byte[] bytes = readFromFile(zipFile);
    unZip(bytes, dir);
    }
    /**
     * 
     * @param files 文件列表
     * @param filePaths 对应的文件压缩路径
     * @return
     */
    public static byte[] zipFileByJavaZip(List<File> files, List<String> filePaths) {
    List<byte[]> fileBytes = new ArrayList<byte[]>(files.size());
    for (int i = 0; i < files.size(); i++) {
    byte[] bytes;
try {
FileInputStream fis = new FileInputStream(files.get(i));
bytes = new byte[(int) files.get(i).length()];
fis.read(bytes);
fis.close();
    fileBytes.add(bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
    }
    return zipByJavaZip(fileBytes, filePaths);
    }
    
    /**
     * 
     * @param unzippedFileBytes 不是压缩文件的字节流列表
     * @param filePaths 问价压缩路径
     * @return
     */
    public static byte[] zipByJavaZip(List<byte[]> unzippedFileBytes, List<String> filePaths) {
        byte[] b = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ZipOutputStream zip = new ZipOutputStream(bos);
            for (int i = 0; i < filePaths.size(); i++) {
           ZipEntry entry = new ZipEntry(filePaths.get(i));
           byte[] fileBytes = unzippedFileBytes.get(i);
           zip.putNextEntry(entry);
           zip.write(fileBytes);
           zip.closeEntry();
            }
            zip.close();
            b = bos.toByteArray();
            bos.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return b;
    
    }
    
    public static void main(String[] args) throws Exception {
    ArrayList<File> files = new ArrayList<File>();
    files.add(new File("./mhxx_configs.xml"));
    files.add(new File("./xlsbz_configs.xml"));
    List<String> filePaths = new ArrayList<String>();
    filePaths.add("scr/mhxx_configs.xml");
    filePaths.add("scr/tmp/xlsbz_configs.xml");
    byte[] bytes = zipFileByJavaZip(files, filePaths);
    FileOutputStream fos = new FileOutputStream("D:\\zz.zip");
    fos.write(bytes);
    fos.flush();
    fos.close();
    unZip(bytes, "d:/");
}

}


ZIP API参考链接


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值