美文网首页
java 使用压缩和解压缩源码

java 使用压缩和解压缩源码

作者: 中v中 | 来源:发表于2021-03-26 19:46 被阅读0次

    压缩源码:

    package com.journaldev.files;
     
    import java.io.File;
    import java.io.FileInputStream;
    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.ZipOutputStream;
     
    public class ZipFiles {
        
        List<String> filesListInDir = new ArrayList<String>();
     
        public static void main(String[] args) {
            File file = new File("/Users/pankaj/sitemap.xml");
            String zipFileName = "/Users/pankaj/sitemap.zip";
            
            File dir = new File("/Users/pankaj/tmp");
            String zipDirName = "/Users/pankaj/tmp.zip";
            
            zipSingleFile(file, zipFileName);
            
            ZipFiles zipFiles = new ZipFiles();
            zipFiles.zipDirectory(dir, zipDirName);
        }
     
        /**
         * This method zips the directory
         * @param dir
         * @param zipDirName
         */
        private void zipDirectory(File dir, String zipDirName) {
            try {
                populateFilesList(dir);
                //now zip files one by one
                //create ZipOutputStream to write to the zip file
                FileOutputStream fos = new FileOutputStream(zipDirName);
                ZipOutputStream zos = new ZipOutputStream(fos);
                for(String filePath : filesListInDir){
                    System.out.println("Zipping "+filePath);
                    //for ZipEntry we need to keep only relative file path, so we used substring on absolute path
                    ZipEntry ze = new ZipEntry(filePath.substring(dir.getAbsolutePath().length()+1, filePath.length()));
                    zos.putNextEntry(ze);
                    //read the file and write to ZipOutputStream
                    FileInputStream fis = new FileInputStream(filePath);
                    byte[] buffer = new byte[1024];
                    int len;
                    while ((len = fis.read(buffer)) > 0) {
                        zos.write(buffer, 0, len);
                    }
                    zos.closeEntry();
                    fis.close();
                }
                zos.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        /**
         * This method populates all the files in a directory to a List
         * @param dir
         * @throws IOException
         */
        private void populateFilesList(File dir) throws IOException {
            File[] files = dir.listFiles();
            for(File file : files){
                if(file.isFile()) filesListInDir.add(file.getAbsolutePath());
                else populateFilesList(file);
            }
        }
     
        /**
         * This method compresses the single file to zip format
         * @param file
         * @param zipFileName
         */
        private static void zipSingleFile(File file, String zipFileName) {
            try {
                //create ZipOutputStream to write to the zip file
                FileOutputStream fos = new FileOutputStream(zipFileName);
                ZipOutputStream zos = new ZipOutputStream(fos);
                //add a new Zip Entry to the ZipOutputStream
                ZipEntry ze = new ZipEntry(file.getName());
                zos.putNextEntry(ze);
                //read the file and write to ZipOutputStream
                FileInputStream fis = new FileInputStream(file);
                byte[] buffer = new byte[1024];
                int len;
                while ((len = fis.read(buffer)) > 0) {
                    zos.write(buffer, 0, len);
                }
                
                //Close the zip entry to write to zip file
                zos.closeEntry();
                //Close resources
                zos.close();
                fis.close();
                fos.close();
                System.out.println(file.getCanonicalPath()+" is zipped to "+zipFileName);
                
            } catch (IOException e) {
                e.printStackTrace();
            }
     
        }
     
    }
    

    结果:

     上面的Java zip示例程序的输出为:
    
        /Users/pankaj/sitemap.xml is zipped to /Users/pankaj/sitemap.zip
        Zipping /Users/pankaj/tmp/.DS_Store
        Zipping /Users/pankaj/tmp/data/data.dat
        Zipping /Users/pankaj/tmp/data/data.xml
        Zipping /Users/pankaj/tmp/data/xmls/project.xml
        Zipping /Users/pankaj/tmp/data/xmls/web.xml
        Zipping /Users/pankaj/tmp/data.Xml
        Zipping /Users/pankaj/tmp/DB.xml
        Zipping /Users/pankaj/tmp/item.XML
        Zipping /Users/pankaj/tmp/item.xsd
        Zipping /Users/pankaj/tmp/ms/data.txt
        Zipping /Users/pankaj/tmp/ms/project.doc
    

    解压源码:

    package com.journaldev.files;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    
    public class UnzipFiles {
    
        public static void main(String[] args) {
            String zipFilePath = "/Users/pankaj/tmp.zip";
            
            String destDir = "/Users/pankaj/output";
            
            unzip(zipFilePath, destDir);
        }
    
        private static void unzip(String zipFilePath, String destDir) {
            File dir = new File(destDir);
            // create output directory if it doesn't exist
            if(!dir.exists()) dir.mkdirs();
            FileInputStream fis;
            //buffer for read and write data to file
            byte[] buffer = new byte[1024];
            try {
                fis = new FileInputStream(zipFilePath);
                ZipInputStream zis = new ZipInputStream(fis);
                ZipEntry ze = zis.getNextEntry();
                while(ze != null){
                    String fileName = ze.getName();
                    File newFile = new File(destDir + File.separator + fileName);
                    System.out.println("Unzipping to "+newFile.getAbsolutePath());
                    //create directories for sub directories in zip
                    new File(newFile.getParent()).mkdirs();
                    FileOutputStream fos = new FileOutputStream(newFile);
                    int len;
                    while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                    }
                    fos.close();
                    //close this ZipEntry
                    zis.closeEntry();
                    ze = zis.getNextEntry();
                }
                //close last ZipEntry
                zis.closeEntry();
                zis.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
    
    }
    

    结果:

    Unzipping to /Users/pankaj/output/.DS_Store
    Unzipping to /Users/pankaj/output/data/data.dat
    Unzipping to /Users/pankaj/output/data/data.xml
    Unzipping to /Users/pankaj/output/data/xmls/project.xml
    Unzipping to /Users/pankaj/output/data/xmls/web.xml
    Unzipping to /Users/pankaj/output/data.Xml
    Unzipping to /Users/pankaj/output/DB.xml
    Unzipping to /Users/pankaj/output/item.XML
    Unzipping to /Users/pankaj/output/item.xsd
    Unzipping to /Users/pankaj/output/ms/data.txt
    Unzipping to /Users/pankaj/output/ms/project.doc
    

    相关文章

      网友评论

          本文标题:java 使用压缩和解压缩源码

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