• apache.commons.compress 压缩,解压


    最近在一个前辈的指引下,开始研究apache.commons。都是网上找的,而且不会中文乱码,而且还可以在压缩包里面加一层文件夹

    package my.test;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
    import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
    import org.apache.commons.compress.archivers.zip.Zip64Mode;
    import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
    import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
    import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
    import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
    import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
    import org.apache.commons.io.FilenameUtils;
    import org.apache.commons.io.IOUtils;
    import org.apache.commons.lang3.StringUtils;
    
    public class FileUtil {
    
        public static int BUFFER_SIZE = 2048;
         public static List<String> unZip(File zipfile, String destDir)
                throws Exception {
            if (StringUtils.isBlank(destDir)) {
                destDir = zipfile.getParent();
            }
            destDir = destDir.endsWith(File.separator) ? destDir : destDir
                    + File.separator;
            ZipArchiveInputStream is = null;
            List<String> fileNames = new ArrayList<String>();
    
            try {
                is = new ZipArchiveInputStream(new BufferedInputStream(
                        new FileInputStream(zipfile), BUFFER_SIZE));
                ZipArchiveEntry entry = null;
                while ((entry = is.getNextZipEntry()) != null) {
                    fileNames.add(entry.getName());
                    if (entry.isDirectory()) {
                        File directory = new File(destDir, entry.getName());
                        directory.mkdirs();
                    } else {
                        OutputStream os = null;
                        try {
                            os = new BufferedOutputStream(new FileOutputStream(
                                    new File(destDir, entry.getName())),
                                    BUFFER_SIZE);
                            IOUtils.copy(is, os);
                        } finally {
                            IOUtils.closeQuietly(os);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            } finally {
                IOUtils.closeQuietly(is);
            }
    
            return fileNames;
        }
    
        public static List<String> unZip(String zipfile, String destDir)
                throws Exception {
            File zipFile = new File(zipfile);
            return unZip(zipFile, destDir);
        }
    
         public static void compressFiles2Zip(File[] files, String zipFilePath) {
            if (files != null && files.length > 0) {
                ZipArchiveOutputStream zaos = null;
                File f = new File(zipFilePath);
                if(f.isDirectory())
                {
                    System.out.println("isDirectory");
                    return;
                }
                if(f.exists())
                {
                    f.delete();
                }
                try {
                    File zipFile = new File(zipFilePath);
                    zaos = new ZipArchiveOutputStream(zipFile);
                    zaos.setUseZip64(Zip64Mode.AsNeeded);
                    int index = 0;
                    for (File file : files) {
                        if (file != null) {
                            ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(
                                    file, new File(file.getParent()).getName()+File.separator+file.getName());
                            zaos.putArchiveEntry(zipArchiveEntry);
                            InputStream is = null;
                            try {
                                is = new BufferedInputStream(new FileInputStream(
                                        file));
                                byte[] buffer = new byte[1024 * 5];
                                int len = -1;
                                while ((len = is.read(buffer)) != -1) {
                                    // 把缓冲区的字节写入到ZipArchiveEntry
                                    zaos.write(buffer, 0, len);
                                }
                                // Writes all necessary data for this entry.
                                zaos.closeArchiveEntry();
                            } catch (Exception e) {
                                throw new RuntimeException(e);
                            } finally {
                                if (is != null)
                                    is.close();
                            }
    
                        }
    
                    }
    
                    zaos.finish();
    
                } catch (Exception e) {
    
                    throw new RuntimeException(e);
    
                } finally {
    
                    try {
    
                        if (zaos != null) {
    
                            zaos.close();
    
                        }
    
                    } catch (IOException e) {
    
                        throw new RuntimeException(e);
    
                    }
    
                }
    
            }
    
        }
    
        public static void main(String[] args) throws Exception {
    
    
            String f  = "C:/20161202/notepad_cache/test/notepad_cache - 副本";
            File file = new File(f);
     
            File[] files = new File[100];
            int index = 0;
            for(String ff : file.list())
            {
                files[index++] = new File(f+File.separator+ff);
                 
            }
            compressFiles2Zip(files, f+".zip");
            /*        
            String f  = "C:/20161202/notepad_cache/test/notepad_cache - 副本";
            unZip(f+"1.zip", "C:/1202/notepad_cache/test/");*/
        }
        
        
                   
    }  
     
  • 相关阅读:
    db2 v11 安装测试
    DB2支持的三种表空间SMS、DMS、DMS的自动存储
    linux几种快速清空文件内容的方法
    修改文件或者文件夹权限
    db2start启动失败
    db2icrt创建实例,提示主机名无效
    浏览器内核以及在各个浏览器的前缀
    程序的三大结构(顺序结构、选择结构、循环结构)
    数组中元素的排序(常用的冒泡排序、选择排序、快速排序)
    数组的api以及api的简单使用
  • 原文地址:https://www.cnblogs.com/rocky-AGE-24/p/6223898.html
Copyright © 2020-2023  润新知