import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.List;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class Compress
{
public static final int BUFFER_LEN=1024;
/*
* @param path the file or directory to be zipped
* @param out the output zip file
* @param name the root file name of the zip file
*/
public static void compress(String path,ZipOutputStream out,String name) throws IOException
{
Path p=Paths.get(path);
if(Files.isRegularFile(p))//if file, then write the bytes into zip file
{
InputStream in=Files.newInputStream(p);
byte[] buff=new byte[BUFFER_LEN];
out.putNextEntry(new ZipEntry(name));
int len=0;
while((len=in.read(buff))!=-1)
{
out.write(buff, 0, len);
}
out.closeEntry();
in.close();
}
else//if directory
{
List<Path> files=Files.list(p).collect(Collectors.toList());//get the file or directory under the current directory
if(files.size()==0)
{
/*
* if no file or directory exists under the current directory, then create the the directory
*/
out.putNextEntry(new ZipEntry(name+"/"));
out.closeEntry();
}
else
{
/*
* if there are other file or directory existing, then do the recursion
*/
for(Path f:files)
compress(f.toString(),out,name+"/"+f.getFileName());
}
}
}
/*
* to zip file
* @param target represents the path of source file
* @param path represents the path of the zip file
*/
public static void zip(String target,String path) throws IOException
{
ZipOutputStream out=new ZipOutputStream(Files.newOutputStream(Paths.get(path)));
try
{
compress(target,out,Paths.get(target).getFileName().toString());
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
out.close();
}
}
/*
* @param path the directory to store the unzipped file
* @param zip the zipped file to be unzipped
*/
public static void decompress(String path,ZipFile zip) throws IOException
{
Enumeration<? extends ZipEntry> entries=zip.entries();//get the entries under the zipfile
while(entries.hasMoreElements())
{
ZipEntry entry=entries.nextElement();
if(entry.isDirectory())//if directory, then create the directory
{
Path dir=Paths.get(path,entry.getName());
if(!Files.exists(dir))
Files.createDirectories(dir);
}
else//if this is a file
{
Path file=Paths.get(path,entry.getName());
Path parent=file.getParent();
if(!Files.exists(parent))
Files.createDirectories(parent);//first create the directory
Files.createFile(file);//create the file
OutputStream out=Files.newOutputStream(file);
InputStream in=zip.getInputStream(entry);
byte[] buff=new byte[BUFFER_LEN];
int len=0;
while((len=in.read(buff))!=-1)//write the content into the created file
{
out.write(buff, 0, len);
}
out.close();
in.close();
}
}
}
/*
*to unzip the zip file
*@param target represents the path of the zip file
*@param path represents the path of the unzip file
*/
public static void unzip(String target,String path) throws IOException
{
ZipFile zip=new ZipFile(target);
try
{
decompress(path,zip);
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
zip.close();
}
}
}
JAVA实现压缩解压
最新推荐文章于 2024-03-30 11:52:44 发布