丵鹰
2017-06-08 01:54
采纳率: 25%
浏览 1.9k
已采纳

读取zip文件,然后根据读取的数据在创建个zip

功能接收zip文件流,然后创建zip文件,现在想自己先写个demo读取zip文件,然后根据读取的数据在创建个zip

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • itjava2017 2017-06-22 07:07
    已采纳
     HttpPost httppost = new HttpPost(url);  
    
                FileBody bin = new FileBody(new File(zipPath));  //zipPath压缩包路径
                StringBody comment = new StringBody(zipName);  
                Charset charSet=Charset.forName("UTF-8");
                StringBody context = new StringBody(obj.toString(),charSet); 
                HttpEntity reqEntity =MultipartEntityBuilder.create().addPart("file", bin).addPart("filename", comment).build();
                httppost.setEntity(reqEntity);
                HttpResponse response = httpclient.execute(httppost);  
                int statusCode = response.getStatusLine().getStatusCode(); 
    
    打赏 评论
  • Hefei19881002 2017-06-08 01:59

    package com.unplatform.vis.util;

    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.zip.CRC32;
    import java.util.zip.CheckedOutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;

    public class ZipFileListUtil {

    static final int BUFFER = 8192;

    private File zipFile;   
    
    public ZipFileListUtil(String pathName) {   
        zipFile = new File(pathName);   
    }   
    public void compress(File[] fileName) { 
        ZipOutputStream out = null;   
        try {  
            FileOutputStream fileOutputStream = new FileOutputStream(zipFile);   
            CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,   
                    new CRC32());   
            out = new ZipOutputStream(cos);   
            String basedir = ""; 
            for (int i=0;i<fileName.length;i++){
                compress(new File(fileName[i].getAbsolutePath()), out, basedir);   
            }
            out.close();  
        } catch (Exception e) {   
            throw new RuntimeException(e);   
        } 
    }   
    public void compress(String srcPathName) {   
        File file = new File(srcPathName);   
        if (!file.exists())   
            throw new RuntimeException(srcPathName + "不存在!");   
        try {   
            FileOutputStream fileOutputStream = new FileOutputStream(zipFile);   
            CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,   
                    new CRC32());   
            ZipOutputStream out = new ZipOutputStream(cos);   
            String basedir = "";   
            compress(file, out, basedir);   
            out.close();   
        } catch (Exception e) {   
            throw new RuntimeException(e);   
        }   
    }   
    
    private void compress(File file, ZipOutputStream out, String basedir) {   
        /* 判断是目录还是文件 */  
        if (file.isDirectory()) {   
            System.out.println("压缩:" + basedir + file.getName());   
            this.compressDirectory(file, out, basedir);   
        } else {   
            System.out.println("压缩:" + basedir + file.getName());   
            this.compressFile(file, out, basedir);   
        }   
    }   
    
    /** 压缩一个目录 */  
    private void compressDirectory(File dir, ZipOutputStream out, String basedir) {   
        if (!dir.exists())   
            return;   
    
        File[] files = dir.listFiles();   
        for (int i = 0; i < files.length; i++) {   
            /* 递归 */  
            compress(files[i], out, basedir + dir.getName() + "/");   
        }   
    }   
    
    /** 压缩一个文件 */  
    private void compressFile(File file, ZipOutputStream out, String basedir) {   
        if (!file.exists()) {   
            return;   
        }   
        try {   
            BufferedInputStream bis = new BufferedInputStream(   
                    new FileInputStream(file));   
            ZipEntry entry = new ZipEntry(basedir + file.getName());   
            out.putNextEntry(entry);   
            int count;   
            byte data[] = new byte[BUFFER];   
            while ((count = bis.read(data, 0, BUFFER)) != -1) {   
                out.write(data, 0, count);   
            }   
            bis.close();   
        } catch (Exception e) {   
            throw new RuntimeException(e);   
        }   
    }   
    

    }

    package com.unplatform.vis.util;

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.zip.GZIPOutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    import java.util.zip.ZipOutputStream;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    public class ZipUtil {
    private static final Logger logger = LoggerFactory.getLogger(ZipUtil.class);
    /**
    * 压缩成ZIP文件
    * zipFileName 压缩后文件路径 inputFile待压缩文件夹目录
    **/
    public static void zip(String zipFileName, File inputFile) throws Exception {
    logger.info("压缩文件夹中...");
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
    BufferedOutputStream bo = new BufferedOutputStream(out);
    //zip(out, inputFile, inputFile.getName(), out);
    zip(out, inputFile, inputFile.getName(), bo);
    bo.close();
    out.close(); // 输出流关闭
    logger.info("压缩文件夹完成");
    }

    public static void zip(ZipOutputStream out, File f, String base,BufferedOutputStream  bo) throws Exception { // 方法重载
        if (f.isDirectory()) {
            File[] fl = f.listFiles();
            if (fl.length == 0) {
                out.putNextEntry(new ZipEntry(base + "/")); // 创建zip压缩进入点base
                //System.out.println(base + "/");
            }
            for (int i = 0; i < fl.length; i++) {
                zip(out, fl[i], base + "/" + fl[i].getName(), bo);// 递归遍历子文件夹
            }
            bo.flush();
        } else {
            out.putNextEntry(new ZipEntry(base)); // 创建zip压缩进入点base
            //System.out.println("-------------base:"+base);
            FileInputStream in = new FileInputStream(f);
            BufferedInputStream bi = new BufferedInputStream(in);
            int b;
            while ((b = bi.read()) != -1) {
                bo.write(b); // 将字节流写入当前zip目录
            }
    
            bo.flush();
    

    // byte[] buf = new byte[1024*1024];
    // while ((b = in.read(buf)) != -1) {
    // bo.write(buf, 0, b);
    // }

            bi.close();
            in.close(); // 输入流关闭
        }
    }
    
    
    
    
    /*解压缩
     * zipFileName 源zip路径 ,unZipPath 输出路径(文件夹目录)
     * */
    public static void unZip(String zipFileName,String unZipPath){
        logger.info("开始解压ZIP文件: "+zipFileName);
        long startTime=System.currentTimeMillis();
        try {
            ZipInputStream zin=new ZipInputStream(new FileInputStream(zipFileName));//输入源zip路径
            BufferedInputStream bin=new BufferedInputStream(zin);
            File Fout=null;
            ZipEntry entry;
            try {
                while((entry = zin.getNextEntry())!=null ){
                    if(entry.isDirectory()){
                        //目录
                        Fout=new File(unZipPath,entry.getName());
                        if(!Fout.exists()){
                            Fout.mkdirs();
                        }
                    }else{
                        //文件
                        Fout=new File(unZipPath,entry.getName());
                        if(!Fout.exists()){
                            (new File(Fout.getParent())).mkdirs();
                        }
                        FileOutputStream out=new FileOutputStream(Fout);
                        BufferedOutputStream Bout=new BufferedOutputStream(out);
                        int b;
                        while((b=bin.read())!=-1){
                            Bout.write(b);
                        }
                        Bout.flush();
                        Bout.close();
                        out.close();
                    }
                    //System.out.println(Fout+"解压成功");  
                }
                bin.close();
                zin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        long endTime=System.currentTimeMillis();
        logger.info("解压ZIP文件耗时: "+(endTime-startTime)+" ms");
    }
    

    // public static void main(String[] args) {
    // ZipUtil book = new ZipUtil();

    // try {
    // //book.zip("d:\zl.zip",new File("d:\zl"));
    // //book.unZip("d:\Huatai2_1.5.zip","e:\");
    // book.zip("Z:\ceshi.zip",new File("Z:\测试输出"));
    //
    // } catch (Exception e) {

    // e.printStackTrace();

    // }

    // }

    /** 
     * 加载本地文件,并转换为byte数组 
     * @return 
     */  
    public static byte[] loadFile(String zipFileName) {  
        File file = new File(zipFileName);  
        FileInputStream fis = null;  
        ByteArrayOutputStream baos = null;  
        byte[] data = null ;  
    
        try {  
            fis = new FileInputStream(file);  
            baos = new ByteArrayOutputStream((int) file.length());  
    
            byte[] buffer = new byte[1024];  
            int len = -1;  
            while ((len = fis.read(buffer)) != -1) {  
                baos.write(buffer, 0, len);  
            }  
    
            data = baos.toByteArray() ;  
    
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if (fis != null) {  
                    fis.close();  
                    fis = null;  
                }  
    
                baos.close() ;  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    
        return data ;  
    } 
    
    /** 
     * 对byte[]进行压缩 
     *  
     * @param 要压缩的数据 
     * @return 压缩后的数据 
     */  
    public static byte[] compress(byte[] data) {  
        System.out.println("before:" + data.length);  
    
        GZIPOutputStream gzip = null ;  
        ByteArrayOutputStream baos = null ;  
        byte[] newData = null ;  
    
        try {  
            baos = new ByteArrayOutputStream() ;  
            gzip = new GZIPOutputStream(baos);  
    
            gzip.write(data);  
            gzip.finish();  
            gzip.flush();  
    
            newData = baos.toByteArray() ;  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                gzip.close();  
                baos.close() ;  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    
        System.out.println("after:" + newData.length);  
        return newData ;  
    }  
    

    }

    打赏 评论

相关推荐 更多相似问题