Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0671acf

Browse files
Add files via upload
0 parents commit 0671acf

File tree

2 files changed

+252
-0
lines changed

2 files changed

+252
-0
lines changed

‎ZipUtil.java

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
}

‎ZipUtils.java

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.cathaynav.dms.util;
2+
3+
import net.lingala.zip4j.core.ZipFile;
4+
import net.lingala.zip4j.model.FileHeader;
5+
6+
import java.io.BufferedReader;
7+
import java.io.File;
8+
import java.io.FileReader;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
/**
13+
* @Author Dengyh
14+
* @Dare 20190403
15+
* @Describe:解压文件工具类
16+
* */
17+
public class ZipUtils {
18+
/**
19+
* 解压方法
20+
* @param zipPath 压缩包所在路径
21+
* @param newPath 解压后要放入的路径
22+
* */
23+
public static void unZip(String zipPath,String newPath){
24+
try{
25+
ZipFile zFile = new ZipFile(zipPath);
26+
zFile.setFileNameCharset("GBK");
27+
File destDir = new File(newPath);
28+
zFile.extractAll(newPath);
29+
List<FileHeader > headerList = zFile.getFileHeaders();
30+
List<File> extractedFileList= new ArrayList<File>();
31+
for(FileHeader fileHeader : headerList) {
32+
33+
if (!fileHeader.isDirectory()) {
34+
extractedFileList.add(new File(destDir,fileHeader.getFileName()));
35+
36+
}
37+
38+
}
39+
File [] extractedFiles = new File[extractedFileList.size()];
40+
extractedFileList.toArray(extractedFiles);
41+
for(File f:extractedFileList){
42+
43+
System.out.println(f.getAbsolutePath()+"....");
44+
45+
}
46+
}catch (Exception e){
47+
e.printStackTrace();
48+
}
49+
}
50+
51+
/**
52+
* 解压方法,获取文件名带有指定名称的文件
53+
* @param zipPath 压缩包所在路径
54+
* @param newPath 解压后要放入的路径
55+
* @param str 包含指定名称
56+
* */
57+
public static File unZipOfUav(String zipPath,String newPath,String str){
58+
try{
59+
ZipFile zFile = new ZipFile(zipPath);
60+
zFile.setFileNameCharset("GBK");
61+
File destDir = new File(newPath);
62+
zFile.extractAll(newPath);
63+
List<FileHeader > headerList = zFile.getFileHeaders();
64+
List<File> extractedFileList= new ArrayList<File>();
65+
for(FileHeader fileHeader : headerList) {
66+
67+
if (!fileHeader.isDirectory()) {
68+
extractedFileList.add(new File(destDir,fileHeader.getFileName()));
69+
70+
}
71+
72+
}
73+
File [] extractedFiles = new File[extractedFileList.size()];
74+
extractedFileList.toArray(extractedFiles);
75+
for(File f:extractedFileList){
76+
if(f.getName().contains(str)){
77+
return f;
78+
}
79+
System.out.println(f.getAbsolutePath()+"....");
80+
81+
}
82+
}catch (Exception e){
83+
e.printStackTrace();
84+
}
85+
return null;
86+
}
87+
/**
88+
* 读文件
89+
* @param file 文件名
90+
* */
91+
public static void readFile(File file){
92+
try(FileReader reader = new FileReader(file.getPath());
93+
BufferedReader br = new BufferedReader(reader)){
94+
String line;
95+
//网友推荐更加简洁的写法
96+
while ((line = br.readLine()) != null) {
97+
// 一次读入一行数据
98+
String[] str = line.split("\t");
99+
System.out.println(line);
100+
}
101+
}catch (Exception e){
102+
e.printStackTrace();
103+
}
104+
}
105+
public static void main(String arg[])throws Exception{
106+
File zip = new File("E:\\pos_X820SN0000360_20181123_173604.zip");
107+
File path = new File("E:\\pos_X820SN0000360_20181123_173604");
108+
// ZipUtil.unZip(zip, path);
109+
String zipPath = "E:\\pos_X820SN0000360_20181123_173604.zip";
110+
String newPath = "E:\\pos_X820SN0000360_20181123_173604\\";
111+
// String zipPath = "E:\12円.07.zip";
112+
// String newPath = "E:\12円.07\\";
113+
File f = unZipOfUav(zipPath,newPath,"pos");
114+
// System.out.println("文件名为:"+file.getPath());
115+
readFile(f);
116+
}
117+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /