内容转载自https://blog.youkuaiyun.com/u014298330/article/details/79976983
引入zip4j
import net.lingala.zip4j.core.ZipFile;
public static void unZip( String zipPath, String destDir ) throws Exception
{
ZipFile zipFile = new ZipFile( zipPath ) ;
zipFile.setFileNameCharset( getEncoding( zipPath ) ) ;
zipFile.extractAll( destDir ) ;
}
@SuppressWarnings( "unchecked" )
private static String getEncoding( String path ) throws Exception
{
String encoding = "GBK" ;
ZipFile zipFile = new ZipFile( path ) ;
zipFile.setFileNameCharset( encoding ) ;
List<FileHeader> list = zipFile.getFileHeaders() ;
for( int i = 0; i < list.size(); i++ )
{
FileHeader fileHeader = list.get( i ) ;
String fileName = fileHeader.getFileName();
if( isMessyCode( fileName ) )
{
encoding = "UTF-8" ;
break ;
}
}
return encoding ;
}
private static boolean isMessyCode( String str )
{
for( int i = 0; i < str.length(); i++ )
{
char c = str.charAt( i ) ;
if( (int)c == 0xfffd )
{
// 存在乱码
return true ;
}
}
return false ;
}
ps:zipPath 是压缩包的位置 destDir 为解压位置