美文网首页
zip文件与byte[]转换

zip文件与byte[]转换

作者: tony_zhang | 来源:发表于2022-02-01 16:39 被阅读0次
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class TestZipFile {
    private static final Logger LOG = LoggerFactory.getLogger(TestZipFile.class);
    public static void main(String[] args) {
        String baseFilePath = "C:\\Users\\Administrator\\Desktop\\folder";

        //1 模拟将zip文件转换成字节数组
        byte[] originFileBytesByFile = getBytesByFile(baseFilePath + "\\testzip1.zip");
        System.out.println("模拟将zip文件转换成字节数组:\r\n" + originFileBytesByFile);

        //2 将zip字节数组转换成zip文件
        getFileByBytes(originFileBytesByFile, baseFilePath, "testzip_1.zip");

        //3 读取zip文件中的txt中的内容输出txt文件字节数组
        byte[] childrenFileBytesByFile = readZipFile(baseFilePath + "\\testzip_1.zip");
        System.out.println("读取zip文件中的txt中的内容输出txt文件字节数组:\r\n" + childrenFileBytesByFile);

        //4 模拟将输出的txt文件字节数组转换成txt文件验证正确性
        getFileByBytes(childrenFileBytesByFile, baseFilePath, "test1.txt");
    }

    /**
     * 直接读取zip文件中的txt中的内容并且输出无乱码
     */
    private static byte[] readZipFile(String file) {
        byte[] bytes = null;
        BufferedReader reader = null;
        ZipInputStream zin = null;
        try {
            ZipFile zf = new ZipFile(file, Charset.forName("GBK"));
            InputStream in = new BufferedInputStream(new FileInputStream(file));
            zin = new ZipInputStream(in, Charset.forName("GBK"));
            ZipEntry ze = null;
            InputStream fis = null;
            while ((ze = zin.getNextEntry()) != null) {
                if (!ze.isDirectory() && !ze.toString().startsWith("__MACOSX")) {
                    long size = ze.getSize();
                    if (size > 0) {
                        //整个代码部分最核心的部分就是这个地方很多乱码的就是这个地方的输入流没有进行编码格式设置
                        reader = new BufferedReader(new InputStreamReader(zin, StandardCharsets.UTF_8));

                        fis = zf.getInputStream(ze);
                        bytes = new byte[file.length()];
                        fis.read(bytes);
                    }
                }
            }
        } catch (IOException e) {
            LOG.info("exception:{}", e.getMessage());
        } finally {
            if (null != reader) {
                try {
                    reader.close();
                } catch (IOException e) {
                    LOG.info("exception:{}", e.getMessage());
                }
            }
            if (null != zin) {
                try {
                    zin.close();
                } catch (IOException e) {
                    LOG.info("exception:{}", e.getMessage());
                }
            }
        }
        return bytes;
    }

    /**
     * 将文件转换成Byte数组
     */
    private static byte[] getBytesByFile(String pathStr) {
        File file = new File(pathStr);
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            byte[] data = bos.toByteArray();
            bos.close();
            return data;
        } catch (IOException e) {
            LOG.info("exception:{}", e.getMessage());
        }
        return null;
    }

    /**
     * 将Byte数组转换成文件
     */
    private static void getFileByBytes(byte[] bytes, String filePath, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        try {
            File dir = new File(filePath);
            if (!dir.exists()) {// 判断文件目录是否存在
                dir.mkdirs();
            }
            File file = new File(filePath + File.separator + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (IOException e) {
            LOG.info("exception:{}", e.getMessage());
        } finally {
            if (null != bos) {
                try {
                    bos.close();
                } catch (IOException e) {
                    LOG.info("exception:{}", e.getMessage());
                }
            }
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    LOG.info("exception:{}", e.getMessage());
                }
            }
        }
    }
}

相关文章

网友评论

      本文标题:zip文件与byte[]转换

      本文链接:https://www.haomeiwen.com/subject/dkgkkrtx.html