|
| 1 | +package com.cathaynav.dms.util; |
| 2 | + |
| 3 | +import org.apache.tools.zip.ZipEntry; |
| 4 | +import org.apache.tools.zip.ZipFile; |
| 5 | + |
| 6 | +import java.io.*; |
| 7 | +import java.util.Enumeration; |
| 8 | +import java.util.zip.CRC32; |
| 9 | +import java.util.zip.CheckedInputStream; |
| 10 | + |
| 11 | +/** |
| 12 | + * @Author Dengyh |
| 13 | + * @Dare 20190403 |
| 14 | + * @Describe:解压文件工具类,有bug,仅支持特定的压缩包 |
| 15 | + * */ |
| 16 | +public class ZipUtil { |
| 17 | + |
| 18 | + |
| 19 | +// public static void unZip(File zipFile, File path) { |
| 20 | +// try { |
| 21 | +// if(!path.exists()) { |
| 22 | +// path.mkdirs(); |
| 23 | +// } |
| 24 | +// FileInputStream fis = new FileInputStream(zipFile); |
| 25 | +// ZipInputStream zis = new ZipInputStream(fis); |
| 26 | +// ZipEntry zipEntry = null; |
| 27 | +// while((zipEntry = zis.getNextEntry()) != null) {//获取条目 |
| 28 | +// String fileName = zipEntry.getName();//获取文件名 |
| 29 | +// File file = new File(path.getAbsolutePath() + "/./" + fileName); |
| 30 | +// ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 31 | +// int len = -1; |
| 32 | +// byte[] buf = new byte[1024]; |
| 33 | +// while((len = zis.read(buf)) != -1) { |
| 34 | +// baos.write(buf, 0, len); |
| 35 | +// } |
| 36 | +// baos.close(); |
| 37 | +// byte[] fileContent = baos.toByteArray();//获取条目内容(即文件内容) |
| 38 | +// FileOutputStream fos = new FileOutputStream(file); |
| 39 | +// fos.write(fileContent); |
| 40 | +// fos.close(); |
| 41 | +// } |
| 42 | +// |
| 43 | +// zis.close(); |
| 44 | +// fis.close(); |
| 45 | +// } catch (Exception e) { |
| 46 | +// e.printStackTrace(); |
| 47 | +// } |
| 48 | +// } |
| 49 | + |
| 50 | + public static File unzip(String zipPath,String newPath){ |
| 51 | + try{ |
| 52 | + ZipFile zipFile = new ZipFile(zipPath,"GBK"); |
| 53 | + //获取压缩文中的所以项 |
| 54 | + for(Enumeration<ZipEntry> enumeration = zipFile.getEntries(); enumeration.hasMoreElements();){ |
| 55 | + ZipEntry zipEntry = enumeration.nextElement();//获取元素 |
| 56 | + //排除空文件夹 |
| 57 | + if(!zipEntry.getName().endsWith(File.separator)) |
| 58 | + { |
| 59 | + System.out.println("正在解压文件:"+zipEntry.getName());//打印输出信息 |
| 60 | + //创建解压目录 |
| 61 | + File f = new File(newPath+zipEntry.getName().substring(0, zipEntry.getName().lastIndexOf("/"))); |
| 62 | + //判断是否存在解压目录 |
| 63 | + if(!f.exists()) |
| 64 | + { |
| 65 | + f.mkdirs();//创建解压目录 |
| 66 | + } |
| 67 | + OutputStream os = new FileOutputStream(newPath+zipEntry.getName());//创建解压后的文件 |
| 68 | + BufferedOutputStream bos = new BufferedOutputStream(os);//带缓的写出流 |
| 69 | + InputStream is = zipFile.getInputStream(zipEntry);//读取元素 |
| 70 | + BufferedInputStream bis = new BufferedInputStream(is);//读取流的缓存流 |
| 71 | + CheckedInputStream cos = new CheckedInputStream(bis, new CRC32());//检查读取流,采用CRC32算法,保证文件的一致性 |
| 72 | +// byte [] b = new byte[1024];//字节数组,每次读取1024个字节 |
| 73 | +// //循环读取压缩文件的值 |
| 74 | +// while(cos.read(b)!=-1) |
| 75 | +// { |
| 76 | +// bos.write(b);//写入到新文件 |
| 77 | +// } |
| 78 | + int readLen = 0; |
| 79 | + byte[] buffer = new byte[1024 * 8]; |
| 80 | + while ((readLen = is.read(buffer, 0, 1024 * 8)) != -1) { |
| 81 | + bos.write(buffer, 0, readLen); |
| 82 | + } |
| 83 | + cos.close(); |
| 84 | + bis.close(); |
| 85 | + is.close(); |
| 86 | + bos.close(); |
| 87 | + os.close(); |
| 88 | + if(zipEntry.getName().contains("pos")){ |
| 89 | + return new File(newPath + zipEntry.getName()); |
| 90 | + } |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + //如果为空文件夹,则创建该文件夹 |
| 95 | + new File(newPath+zipEntry.getName()).mkdirs(); |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + }catch (Exception e){ |
| 102 | + e.printStackTrace(); |
| 103 | + } |
| 104 | + return null; |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + public static void handleFile(File file){ |
| 109 | + try(FileReader reader = new FileReader(file.getPath()); |
| 110 | + BufferedReader br = new BufferedReader(reader)){ |
| 111 | + String line; |
| 112 | + //网友推荐更加简洁的写法 |
| 113 | + while ((line = br.readLine()) != null) { |
| 114 | + // 一次读入一行数据 |
| 115 | + String[] str = line.split("\t"); |
| 116 | + System.out.println(line); |
| 117 | + } |
| 118 | + }catch (Exception e){ |
| 119 | + e.printStackTrace(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public static void main(String arg[])throws Exception{ |
| 124 | + File zip = new File("E:\\pos_X820SN0000360_20181123_173604.zip"); |
| 125 | + File path = new File("E:\\pos_X820SN0000360_20181123_173604"); |
| 126 | +// ZipUtil.unZip(zip, path); |
| 127 | +// String zipPath = "E:\\pos_X820SN0000360_20181123_173604.zip"; |
| 128 | +// String newPath = "E:\\pos_X820SN0000360_20181123_173604\\"; |
| 129 | + String zipPath = "E:\\12.07.zip"; |
| 130 | + String newPath = "E:\\12.07\\"; |
| 131 | + File file = unzip(zipPath,newPath); |
| 132 | + System.out.println("文件名为:"+file.getPath()); |
| 133 | + handleFile(file); |
| 134 | + } |
| 135 | +} |
0 commit comments