zoukankan      html  css  js  c++  java
  • zip解压文件java

    </pre><pre name="code" class="java">/**
    通过查阅API帮助文档,在java.util.zip包中找到所需进行压缩解压的操作类。
    1、解压文件:ZipInputStream
    构造方法:
    ZipInputStream(InputStream in)
    所需方法:
    1、void close()关闭流操作
    2、void closeEntry()关闭当前ZIP条目并定位流以操作下一个条目。
    3、ZipEntry getNextEntry()读取下一个zip条目,并将流定位到该条目数据开始的地方。
    4、read(byte[] b, int off, int len) 从当前 ZIP 条目读入字节数组。
    介绍下ZipEntry类:
    构造方法:
    ZipEntry(String name)使用指定名称创建新的条目
    常用方法:
    1、getName()获取条目名称
    2、getSize()返回条目压缩大小
    3、isDirectory()是否为目录条目
    ZipFile函数:
    1、void	close() 关闭 ZIP 文件。
    2、Enumeration<? extends ZipEntry>   entries() 返回 ZIP 文件条目的枚举。
    3、protected  void	finalize() 确保不再引用此 ZIP 文件时调用它的 close 方法。
    4、ZipEntry  getEntry(String name)  返回指定名称的 ZIP 文件条目;如果未找到,则返回 null。
    5、InputStream getInputStream(ZipEntry entry) 返回输入流以读取指定 ZIP 文件条目的内容。
    6、String getName() 返回 ZIP 文件的路径名。
    7、int size()返回 ZIP 文件中的条目数。
    */
    import java.io.*;
    import java.util.Enumeration;
    import java.util.zip.*;;
    import java.lang.Exception.*;
    class Zip 
    {
    	private String zipFile;
    	private String zipFilePath;
    	Zip(String zipFile,String zipFilePath)
    	{
    		this.zipFile = zipFile;
    		this.zipFilePath = zipFilePath;
    	}
    
    	/*
    		利用zipEntry类进行解压文件的使用,通过zipEntry的方法获取文件的大小、名称,
    		利用zipInputStream的方法来读取文件进行。
    	*/
    	public boolean unzipFile()
    	{
    		try
    		{
    			File zipfile = new File(zipFile);
    			File unzipfile = new File(zipFilePath);
    			if(!zipfile.exists())
    			{
    				System.out.println("解压文件不存在,请检查文件路径。");
    				return false;
    			}
    
    			ZipInputStream zipin = new ZipInputStream(new FileInputStream(zipfile));
    			ZipEntry zipEn = null;
    			while((zipEn = zipin.getNextEntry()) != null)
    			{
    				String fileName = zipEn.getName();
    				if(zipEn.isDirectory())
    				{
    					File dirfile = new File(unzipfile,fileName);
    					System.out.println("正在创建文件夹【"+ fileName + "】");
    					dirfile.mkdirs();
    				}
    				else
    				{
    					System.out.println("正在解压文件【" + fileName +"】");
    					File temfile = new File(unzipfile,fileName);
    					FileOutputStream fos = new FileOutputStream(temfile);
    					byte[] buf =new byte[1024];
    					int len = 0;
    					while((len = zipin.read(buf)) != -1)
    						fos.write(buf,0,len);
    
    					//关闭当前条目
    					zipin.closeEntry();
    				}
    			}
    
    			//关闭与资源关联的所有流
    			zipin.close();
    			return true;
    		}
    		catch (Exception ex)
    		{
    			throw new RuntimeException("解压文件错误。");
    		}
    	}
    
    	/*
    	利用ZipFile类中的方法对文件进行读取。
    	步骤:
    	1、通过ZipFile的entries()返回 ZIP 文件条目的枚举。
    	2、 ZipEntry	getEntry(String name) 返回指定名称的 ZIP 文件条目;如果未找到,则返回 null。
    	3、通过 InputStream	getInputStream(ZipEntry entry) 返回输入流以读取指定 ZIP 文件条目的内容。	
    	*/
    	public boolean unzipFile2()
    	{
    		try
    		{
    			File zipfile = new File(zipFile);
    			File unzipfile = new File(zipFilePath);
    			if(!zipfile.exists())
    			{
    				System.out.println("解压文件不存在,请检查文件路径。");
    				return false;
    			}
    			ZipFile zfile= new ZipFile(zipfile);
    			Enumeration<? extends ZipEntry> zipEntries = zfile.entries();
    			ZipEntry zipEntry = null;
    			while(zipEntries.hasMoreElements())
    			{
    				zipEntry = zipEntries.nextElement();
    				InputStream in = zfile.getInputStream(zipEntry);
    				String fileName = zipEntry.getName();
    				if(zipEntry.isDirectory())
    				{
    					File dirfile = new File(unzipfile,fileName);
    					System.out.println("正在创建文件夹【"+ fileName + "】");
    					dirfile.mkdirs();
    				}
    				else
    				{
    					System.out.println("正在解压文件【" + fileName +"】");
    					File temFile = new File(unzipfile,fileName);
    					FileOutputStream fos = new FileOutputStream(temFile);
    					byte[] buf =new byte[1024];
    					int len = 0;
    					while((len = in.read(buf)) != -1)
    						fos.write(buf,0,len);
    					
    					fos.close();
    				}
    
    				in.close();
    				return true;
    			}
    			zfile.close();
    			return true;
    		}
    		catch (Exception ex)
    		{
    			throw new RuntimeException("解压文件错误。");
    		}
    		
    	}
    }
    class ZipDemo 
    {
    	public static void main(String[] args) throws Exception
    	{
    		if(args.length != 2)
    		{
    			System.out.println("请输入压缩文件路径和保存路径。");
    			return;
    		}
    		Zip zip = new Zip(args[0],args[1]);
    		if(zip.unzipFile2())
    			System.out.println("解压成功。");
    		else 
    			System.out.println("解压失败.");
    	}
    }
    


  • 相关阅读:
    【Android开发艺术探索】Activity的生命周期和启动模式
    【Android】基于WanAndroid开放API实现的文章阅读APP
    【Android】天气应用
    【Android】动态更新Icon
    【Android】VirtualAPK的简单使用
    Android数据存储之SD卡文件操作
    Android数据存储之SQLite数据库
    Android数据存储之共享参数SharedPreferences
    tensor维度变换
    tensor数据基操----索引与切片
  • 原文地址:https://www.cnblogs.com/dengshiwei/p/4258416.html
Copyright © 2011-2022 走看看