当前位置:网站首页>Gzip compression decompression

Gzip compression decompression

2022-06-12 21:33:00 Besieged city_ city with high walls

package com.raise.raisestudy.zip;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;


public class GzipUtils {
    

  /** *  Compress a file or directory <br/> *  Compress the file or directory into zip Format of compressed files  * * @param compressedFile  Compressed file  * @param preCompressFile  The path to a file or directory that needs to be compressed  * @return true | false */  
    public static boolean compress(String compressedFile, String preCompressFile)  
    {
      
        boolean isSucc = true;  
        File inputFile = new File(preCompressFile);  
        ZipOutputStream out;  
        try  
        {
      
            out = new ZipOutputStream(new FileOutputStream(compressedFile));  
            compress(out, inputFile, inputFile.getName());  
            out.close();  
        }  
        catch (Exception e)  
        {
      
            isSucc = false;    
        }  
        return isSucc;  
    }  
      
    /** *  Recursively compress all files in the folder  * * @param out  Output stream  * @param file  Files that need to be compressed  * @param base  The parent directory of the compressed file  * @throws IOException */  
    private static void compress(ZipOutputStream out, File file, String base)  
            throws IOException  
    {
      
        if (file.isDirectory())  
        {
      
            File[] fs = file.listFiles();  
            base += "/";  
            out.putNextEntry(new ZipEntry(base)); //  Generate the corresponding directory  
            for (int i = 0; i < fs.length; i++)  
            {
      
                //  Recursively traverse all file objects in this directory , Compress one by one  
                compress(out, fs[i], base + fs[i].getName());  
            }  
              
        }  
        else  
        {
      
            out.putNextEntry(new ZipEntry(base));  
            InputStream is = new FileInputStream(file);  
            byte[] buf = new byte[1024];  
            int len = 0;  
            while ((len = is.read(buf)) != -1)  
            {
      
                out.write(buf, 0, len);  
            }  
            is.close();  
        }  
    }  
      
    /** *  Unzip the file <br/> *  decompression zip File format  * @param zipFile  Files that need to be decompressed  * @param desPath  Directory saved after decompression  */  
    public static void decompress(String zipFile, String desPath) throws IOException 
    {
      
        //  Create output stream , Used to write the file stream read from the compressed file to the disk  
        OutputStream out = null;  
        //  Create an input stream , Used to read files from compressed files  
        ZipInputStream is;  
        File dir = new File(desPath);  
        dir.mkdirs();  
            is = new ZipInputStream(new FileInputStream(zipFile));  
            ZipEntry entry = null;  
            while ((entry = is.getNextEntry()) != null)  
            {
      
                File f = new File(dir, entry.getName());  
                if (entry.isDirectory())  
                {
      
                    f.mkdir();  
                }  
                else  
                {
      
                    //  Create a new file according to the file name read out in the compressed file  
                    out = new FileOutputStream(f);  
                    byte[] buf = new byte[1024];  
                    int len = 0;  
                    while ((len = is.read(buf)) != -1)  
                    {
      
                        out.write(buf, 0, len);  
                    }  
                    out.close();  
                }  
            }  
              
            is.close();  
    }  
}
原网站

版权声明
本文为[Besieged city_ city with high walls]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206122125175781.html