不解压直接读取压缩包中的文件

参考:

http://www.cnblogs.com/kgdxpr/archive/2013/05/20/3088254.html

http://blog.csdn.net/zshake/article/details/40344183

https://segmentfault.com/a/1190000003059143

 

内容:

//编码格式UTF-8
package test;

import java.io.BufferedInputStream;  
import java.io.BufferedReader;  
import java.io.FileInputStream;  
import java.io.InputStream;  
import java.io.InputStreamReader;  
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;  
import java.util.zip.ZipFile;  
import java.util.zip.ZipInputStream;



public class Test {

    public static void main(String[] args) throws Exception {
        try {  
               readZipFile("C:\\Users\\Administrator\\Desktop\\20170428\\1.gz");  
           } catch (Exception e) {  
               // TODO Auto-generated catch block  
               e.printStackTrace();  
           }  
    }
    
    public static void readZipFile(String file) throws Exception {  
         //使用ZipInputStream解压Zip文件  
    	/*ZipFile zf = new ZipFile(file);  
           InputStream in = new BufferedInputStream(new FileInputStream(file));  
           ZipInputStream zin = new ZipInputStream(in);  
           ZipEntry ze;  
           while ((ze = zin.getNextEntry()) != null) {  
               if (ze.isDirectory()) {
               } else {  
                   System.err.println("file - " + ze.getName() + " : "  
                           + ze.getSize() + " bytes");  
                   long size = ze.getSize();  
                   if (size > 0) {  
                       BufferedReader br = new BufferedReader(  
                               new InputStreamReader(zf.getInputStream(ze)));  
                       String line;  
                       while ((line = br.readLine()) != null) {  
                           System.out.println(line);  
                       }  
                       br.close();  
                   }  
                   System.out.println();  
               }  
           }  
           zin.closeEntry();
           */
    	//int lable = signLine(3);
//    	System.out.println(lable+",/ASO100");
    	
    	//使用GZIPInputStream解压GZ文件 
        InputStream in = new GZIPInputStream(new FileInputStream(file));  
        Scanner sc=new Scanner(in);  
        List<String> lines=new ArrayList();
        
    	//int flag = 0;
        
        while(sc.hasNextLine()){
/*        	flag++;
        	if(flag == lable) {
        		lines.add(sc.nextLine());
        		System.out.println( sc.nextLine());
//        		System.out.println(lines.toString());
        		break;
        	}
        	else
        		continue;*/
//        	String s = sc.nextLine();
        	System.out.println( sc.nextLine());
//        	String g = lable+",/ASO100";
//            System.out.println(sc.nextLine());
//        		lines.add(sc.nextLine());
        	
        }
        
/*    	if(lines.toString().matches(lable+",/ASO100"))
    	System.out.println(lines.toString());
        in.close();*/
       }

/*	private static int signLine(int i) {
		// TODO Auto-generated method stub

    	System.out.println("打印第 " + i + " 行数据");
		//return (i-1);
	} */ 
}

  

posted @ 2017-05-19 11:35  tangyongathuse  阅读(1185)  评论(0编辑  收藏  举报