java 实现解压zip 包
javascript">
< ! -- ZIP / RAR 压缩文件解压-- >
< ! -- https: / / mvnrepository. com/ artifact/ ant/ ant -- >
< dependency>
< groupId> ant< / groupId>
< artifactId> ant< / artifactId>
< version> 1.6 .5 < / version>
< / dependency>
package org. hwdz. common. util;
import com. alibaba. fastjson. JSONObject;
import java . io. * ;
import java . util. ArrayList;
import java . util. Enumeration;
import java . util. List;
import java . util. zip . ZipEntry;
import java . util. zip . ZipFile;
public class DecompressingUtils {
public static void zip Uncompress( String inputFile, String uncompress) throws Exception {
File srcFile = new File ( inputFile) ;
if ( ! srcFile. exists ( ) ) {
throw new Exception ( srcFile. getPath ( ) + "所指文件不存在" ) ;
}
String destDirPath = inputFile. replace ( ".zip " , "" ) ;
ZipFile zip File = new ZipFile ( srcFile) ;
Enumeration< ? > entries = zip File. entries ( ) ;
while ( entries. hasMoreElements ( ) ) {
ZipEntry entry = ( ZipEntry) entries. nextElement ( ) ;
if ( entry. isDirectory ( ) ) {
srcFile. mkdirs ( ) ;
} else {
File targetFile = new File ( uncompress + "/" + entry. getName ( ) ) ;
if ( ! targetFile. getParentFile ( ) . exists ( ) ) {
targetFile. getParentFile ( ) . mkdirs ( ) ;
}
targetFile. createNewFile ( ) ;
InputStream is = zip File. getInputStream ( entry) ;
FileOutputStream fos = new FileOutputStream ( targetFile) ;
int len;
byte[ ] buf = new byte [ 1024 ] ;
while ( ( len = is. read ( buf) ) != - 1 ) {
fos. write ( buf, 0 , len) ;
}
fos. close ( ) ;
is. close ( ) ;
}
}
}
public static void readFiles ( String inputFile) throws Exception {
File srcFile = new File ( inputFile) ;
if ( srcFile. isDirectory ( ) ) {
File next[ ] = srcFile. listFiles ( ) ;
for ( int i = 0 ; i < next. length; i++ ) {
System. out. println ( next[ i] . getName ( ) ) ;
if ( ! next[ i] . isDirectory ( ) ) {
BufferedReader br = new BufferedReader ( new FileReader ( next[ i] ) ) ;
List< String> arr1 = new ArrayList < > ( ) ;
String contentLine ;
while ( ( contentLine = br. readLine ( ) ) != null ) {
JSONObject js = JSONObject. parseObject ( contentLine) ;
arr1. add ( contentLine) ;
}
}
}
}
}
public static void main ( String[ ] args ) {
try {
String path = "F:\\etc\\dist(1).zip " ;
zip Uncompress( path, "F:\\etc\\test" ) ;
readFiles ( path. replace ( ".zip " , "" ) ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
}