1.代码示例
javascript">import java . io. IOException;
import java . io. InputStream;
import java . io. OutputStream;
import java . nio. file. * ;
import java . nio. file. attribute. BasicFileAttributes;
import java . util. LinkedList;
import java . util. stream. Collectors;
import java . util. zip . ZipEntry;
import java . util. zip . ZipFile;
import java . util. zip . ZipInputStream;
import java . util. zip . ZipOutputStream;
public class ZipUtil {
public static void unPacket ( Path file, Path targetDir) throws IOException {
if ( ! Files. exists ( targetDir) ) {
Files. createDirectories ( targetDir) ;
}
ZipFile zip File = new ZipFile ( file. toFile ( ) ) ;
try {
try ( ZipInputStream zip InputStream = new ZipInputStream ( Files. newInputStream ( file) ) ) {
ZipEntry zip Entry = null ;
while ( ( zip Entry = zip InputStream. getNextEntry ( ) ) != null ) {
String entryName = zip Entry. getName ( ) ;
Path entryFile = targetDir. resolve ( entryName) ;
if ( zip Entry. isDirectory ( ) ) {
if ( ! Files. isDirectory ( entryFile) ) {
Files. createDirectories ( entryFile) ;
}
} else {
try ( InputStream zip EntryInputStream = zip File. getInputStream ( zip Entry) ) {
try ( OutputStream fileOutputStream = Files. newOutputStream ( entryFile, StandardOpenOption. CREATE_NEW ) ) {
byte[ ] buffer = new byte [ 4096 ] ;
int length = 0 ;
while ( ( length = zip EntryInputStream. read ( buffer) ) != - 1 ) {
fileOutputStream. write ( buffer, 0 , length) ;
}
fileOutputStream. flush ( ) ;
}
}
}
}
}
} finally {
zip File. close ( ) ;
}
}
public static void packet ( Path[ ] files, Path zip File) throws IOException {
OutputStream outputStream = Files. newOutputStream ( zip File, StandardOpenOption. CREATE_NEW ) ;
ZipOutputStream zip OutputStream = new ZipOutputStream ( outputStream) ;
try {
for ( Path file : files) {
if ( Files. isDirectory ( file) ) {
continue ;
}
try ( InputStream inputStream = Files. newInputStream ( file) ) {
ZipEntry zip Entry = new ZipEntry ( file. getFileName ( ) . toString ( ) ) ;
zip OutputStream. putNextEntry ( zip Entry) ;
int len = 0 ;
byte[ ] buffer = new byte [ 1024 * 10 ] ;
while ( ( len = inputStream. read ( buffer) ) > 0 ) {
zip OutputStream. write ( buffer, 0 , len) ;
}
zip OutputStream. flush ( ) ;
}
}
zip OutputStream. closeEntry ( ) ;
} finally {
zip OutputStream. close ( ) ;
outputStream. close ( ) ;
}
}
public static void packet ( Path folder, Path zip File) throws IOException {
if ( ! Files. isDirectory ( folder) ) {
throw new IllegalArgumentException ( folder. toString ( ) + " 不是合法的文件夹" ) ;
}
OutputStream outputStream = Files. newOutputStream ( zip File, StandardOpenOption. CREATE_NEW ) ;
ZipOutputStream zip OutputStream = new ZipOutputStream ( outputStream) ;
LinkedList< String> path = new LinkedList < > ( ) ;
try {
Files. walkFileTree ( folder, new FileVisitor < Path> ( ) {
@Override
public FileVisitResult preVisitDirectory ( Path dir, BasicFileAttributes attrs) throws IOException {
if ( ! dir. equals ( folder) ) {
String folder = dir. getFileName ( ) . toString ( ) ;
path. addLast ( folder) ;
ZipEntry zip Entry = new ZipEntry ( path. stream ( ) . collect ( Collectors. joining ( "/" , "" , "/" ) ) ) ;
try {
zip OutputStream. putNextEntry ( zip Entry) ;
zip OutputStream. flush ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
return FileVisitResult. CONTINUE ;
}
@Override
public FileVisitResult visitFile ( Path file, BasicFileAttributes attrs) throws IOException {
try ( InputStream inputStream = Files. newInputStream ( file) ) {
String fileName = path. size ( ) > 0
? path. stream ( ) . collect ( Collectors. joining ( "/" , "" , "" ) ) + "/" + file. getFileName ( ) . toString ( )
: file. getFileName ( ) . toString ( ) ;
ZipEntry zip Entry = new ZipEntry ( fileName) ;
zip OutputStream. putNextEntry ( zip Entry) ;
int len = 0 ;
byte[ ] buffer = new byte [ 1024 * 10 ] ;
while ( ( len = inputStream. read ( buffer) ) > 0 ) {
zip OutputStream. write ( buffer, 0 , len) ;
}
zip OutputStream. flush ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
return FileVisitResult. CONTINUE ;
}
@Override
public FileVisitResult visitFileFailed ( Path file, IOException exc) throws IOException {
return FileVisitResult. CONTINUE ;
}
@Override
public FileVisitResult postVisitDirectory ( Path dir, IOException exc) throws IOException {
if ( ! path. isEmpty ( ) ) {
path. removeLast ( ) ;
}
return FileVisitResult. CONTINUE ;
}
} ) ;
zip OutputStream. closeEntry ( ) ;
} finally {
zip OutputStream. close ( ) ;
outputStream. close ( ) ;
}
}
}