JAVA实现压缩解压

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();
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值