java的解压缩

//解压文件
//需要用到apache的ant.jar包,保证不中文乱码
public class Test_JY
{
static String zipPath = “D:/Test.zip”;//需要解压的压缩文件
static String outPath = “D:/movie/视频”;//解压完成后保存路径,记得"\"结尾哈

public static void main(String args[]) throws Exception
{
	ZipFile zipFile = new ZipFile(zipPath,"GBK");//压缩文件的实列,并设置编码
	//获取压缩文中的所以项
	for(Enumeration<ZipEntry> enumeration = zipFile.getEntries();enumeration.hasMoreElements();)
	{
		ZipEntry zipEntry = enumeration.nextElement();//获取元素
		//排除空文件夹
		if(!zipEntry.getName().endsWith(File.separator))
		{
			System.out.println("正在解压文件:"+zipEntry.getName());//打印输出信息
			//创建解压目录
			File f = new File(outPath+zipEntry.getName().substring(0, zipEntry.getName().lastIndexOf(File.separator)));
			//判断是否存在解压目录
			if(!f.exists())
			{
				f.mkdirs();//创建解压目录
			}
			OutputStream os = new FileOutputStream(outPath+zipEntry.getName());//创建解压后的文件
			BufferedOutputStream bos = new BufferedOutputStream(os);//带缓的写出流
			InputStream is = zipFile.getInputStream(zipEntry);//读取元素
			BufferedInputStream bis = new BufferedInputStream(is);//读取流的缓存流
			CheckedInputStream cos = new CheckedInputStream(bis, new CRC32());//检查读取流,采用CRC32算法,保证文件的一致性
			byte [] b = new byte[1024];//字节数组,每次读取1024个字节
			//循环读取压缩文件的值
			while(cos.read(b)!=-1)
			{
				bos.write(b);//写入到新文件
			}
			cos.close();
			bis.close();
			is.close();
			bos.close();
			os.close();
		}
		else
		{
			//如果为空文件夹,则创建该文件夹
			new File(outPath+zipEntry.getName()).mkdirs();
		}
	}
	System.out.println("解压完成");
	zipFile.close();
}

}

//压缩文件
public class Test_YS
{
static String filePath = “E:/壁纸”;//需要压缩的文件夹完整路径
static String fileName = “壁纸”;//需要压缩的文件夹名
static String outPath = “D:/Test.zip”;//压缩完成后保存为Test.zip文件,名字随意

public static void main(String args[]) throws Exception
{
	OutputStream os = new FileOutputStream(outPath);//创建Test.zip文件
	CheckedOutputStream cos = new CheckedOutputStream(os, new CRC32());//检查输出流,采用CRC32算法,保证文件的一致性
	ZipOutputStream zos = new ZipOutputStream(cos);//创建zip文件的输出流
	zos.setEncoding("GBK");//设置编码,防止中文乱码
	File file = new File(filePath);//需要压缩的文件或文件夹对象
	ZipFile(zos,file);//压缩文件的具体实现函数
	zos.close();
	cos.close();
	os.close();
	System.out.println("压缩完成");
}

//递归,获取需要压缩的文件夹下面的所有子文件,然后创建对应目录与文件,对文件进行压缩
public static void ZipFile(ZipOutputStream zos,File file) throws Exception
{
	if(file.isDirectory())
	{
		//创建压缩文件的目录结构
		zos.putNextEntry(new ZipEntry(file.getPath().substring(file.getPath().indexOf(fileName))+File.separator));

		for(File f : file.listFiles())
		{
			ZipFile(zos,f);
		}
	}else{
		//打印输出正在压缩的文件
		System.out.println("正在压缩文件:"+file.getName());
		//创建压缩文件
		zos.putNextEntry(new ZipEntry(file.getPath().substring(file.getPath().indexOf(fileName))));
		//用字节方式读取源文件
		InputStream is = new FileInputStream(file.getPath());
		//创建一个缓存区
		BufferedInputStream bis = new BufferedInputStream(is);
		//字节数组,每次读取1024个字节
		byte [] b = new byte[1024];
		//循环读取,边读边写
		while(bis.read(b)!=-1)
		{
			zos.write(b);//写入压缩文件
		}
		//关闭流
		bis.close();
		is.close();
	}
}

}

猜你喜欢

转载自blog.csdn.net/good_luck_l/article/details/88303715
今日推荐