Java automatically decompresses the file instance code

  • 2020-04-01 01:43:53
  • OfStack


import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.Enumeration; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipFile;

public class UnZipper {
     
    @SuppressWarnings("rawtypes") 
    public static void unZipFiles(File zipFile)throws IOException{ 
        //Get the directory where the compressed files are located
        String path=zipFile.getAbsolutePath(); 
        path=path.substring(0,path.lastIndexOf("\")); 
       // System.out.println("path "+path); 
        ZipFile zip = new ZipFile(zipFile); 
        for(Enumeration entries =zip.entries(); 
                entries.hasMoreElements();){ 
            ZipEntry entry = (ZipEntry)entries.nextElement(); 
            String zipEntryName = entry.getName(); 
            InputStream in = zip.getInputStream(entry); 
            //OutPath outputs the directory
            String outPath = (path+"\"+zipEntryName).replaceAll("\*", "/");; 
            //System.out.println("outPath "+outPath); 
            //Determines if the path exists, and creates the file path if it does not
            File file = new File(outPath.substring(0, outPath.lastIndexOf('/'))); 
            if(!file.exists()){ 
                file.mkdirs(); 
            } 
            //Determine whether the full path of the file is a folder, if it has been uploaded above, do not need to unzip
            if(new File(outPath).isDirectory()){ 
                continue; 
            } 
            //Output file path information
            System.out.println(outPath); 

            OutputStream out = new FileOutputStream(outPath); 
            byte[] buf1 = new byte[1024]; 
            int len; 
            while((len=in.read(buf1))>0){ 
                out.write(buf1,0,len); 
            } 
            in.close(); 
            out.close(); 
            } 
        System.out.println("****************** Extract the complete ********************"); 
    } 
    
    public static void main(String[] args) { 
        try { 
            unZipFiles(new File("D:\all\zip\Default.adiumemoticonset.zip")); 
        } catch (IOException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
    } 
} 


Related articles: