package com.tax.file.test;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;


import com.yxcheng.token.utils.ExcelReadUtil;
import org.junit.Test;

public class TestReadFile {

	public static void main(String[] args){

		ZipInputStream zipInputStream = null;
		InputStream inputStream = null;
		try {
			long startTime = System.currentTimeMillis();
			
			String urlAddr = "http://192.168.6.14:9000/base/8ad1a3c86ccd8c7f016ccd8c85530002.zip?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=VJB6TLQYU1JIX2XKC332%2F20190828%2F%2Fs3%2Faws4_request&X-Amz-Date=20190828T030403Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=17ca9be7d8c8460884e9c4c5462c70aa6654b10db896ad1fe4a2a4391f86965a";
			int timeout = 10*1000;
			inputStream = ExcelReadUtil.getInputStreamByUrl(urlAddr, timeout); //得到输入流
			
			System.out.println("获取URL输入流耗时："+(System.currentTimeMillis()-startTime));
			
			zipInputStream = new ZipInputStream(inputStream);
			ZipEntry zipEntry = null;
			
			//循环遍历
			while ((zipEntry = zipInputStream.getNextEntry()) != null) {
     
			    System.out.println("文件名：" + zipEntry.getName());
			   
			    InputStream inputStreamClone =  ExcelReadUtil.inputStreamClone(zipInputStream);
			     
			    System.out.println("克隆输入流耗时："+(System.currentTimeMillis()-startTime));
			    
			    List<String[]> list = ExcelReadUtil.readExcel(inputStreamClone);
			    System.out.println("读取Excel流耗时："+(System.currentTimeMillis()-startTime));
			    
			    if(list == null || list.size() == 0){
			    	continue;
			    }
			    
			    int size = list.size();
			    for(int i = 0;i<size;i++){
			    	String line = null;
			    	int sizesub = list.get(i).length;
			    	for(int j = 0;j<sizesub;j++){
			    		line += list.get(i)[j]+",";
			    	}
			    	System.out.println(line);
			    }
			    
			}
			
			long endTime = System.currentTimeMillis();
			
			System.out.println("耗时："+(endTime-startTime));
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(zipInputStream != null){
				try {
					zipInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		System.out.println();
        
	}



	@Test
	public void testReadFile() throws Exception {
	    String path = "/Users/wenhao/Downloads/8ad1a3c870e6ca970170e9702860009a/a8d78c22754746a080a4560272d2c5f2.xls";
        File file = new File(path);
        System.out.println(file.canRead());
        FileInputStream fis = new FileInputStream(file);
        byte[]b = new byte[1];
        fis.read(b);
    }
	
}
