`
knight_black_bob
  • 浏览: 824353 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

gzip unzip java 压缩 解压缩 bytes

    博客分类:
  • java
阅读更多

 

 

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipInputStream;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

public class ZipUtil
{
	 
	static void createDir(String path)
	{
		File dir = new File(path);
		if (dir.exists() == false)
			dir.mkdir();
	}

	 
	static String getSuffixName(String name)
	{
		return name.substring(0, name.lastIndexOf("."));
	}
 
	public static void unzip(File src, File dstDir) throws IOException
	{
		ZipFile zipFile = null;
		try
		{
			// 创建zip文件对象
			//zipFile = new ZipFile(src);
			 if(System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0){  
	             zipFile = new ZipFile(src,"GBK");  
	         }else if(System.getProperty("os.name").toLowerCase().indexOf("linux") >= 0){  
	             //zipFile = new ZipFile(src,"GB2312");
	        	 zipFile = new ZipFile(src,"GBK");
	         }  
			// 得到zip文件条目枚举对象
			Enumeration zipEnum = zipFile.getEntries();
			// 定义输入输出流对象
			// 定义对象
			ZipEntry entry = null;
			// 循环读取条目
			while (zipEnum.hasMoreElements())
			{
				// 得到当前条目
				entry = (ZipEntry) zipEnum.nextElement();
				String entryName = new String(entry.getName());			
				// 用/分隔条目名称
				String names[] = entryName.split("\\/");
				int length = names.length;
				String path = dstDir.getAbsolutePath();
				for (int v = 0; v < length; v++)
				{
					if (v < length - 1)
					{ // 最后一个目录之前的目录
						path += "/" + names[v] + "/";
						createDir(path);
					}
					else
					{ // 最后一个
						if (entryName.endsWith("/")) // 为目录,则创建文件夹
							createDir(dstDir.getAbsolutePath() + "/" + entryName);
						else
						{
							InputStream input = null;
							OutputStream output = null;
							try
							{// 为文件,则输出到文件
								input = zipFile.getInputStream(entry); 
								output = new FileOutputStream(new File(dstDir.getAbsolutePath() + "/" + entryName));
								byte[] buffer = new byte[1024 * 8];
								int readLen = 0;
								while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1)
									output.write(buffer, 0, readLen);
								// 关闭流
								output.flush();
							}
							finally
							{
								if (input != null)
									input.close();
								if (output != null)
									output.close();
							}
						}
					}
				}
			}
		}
		finally
		{
			if (zipFile != null)
				zipFile.close();
		}
	}
 
	public static void unzip(String zipFilePath, String unzipDirectory) throws Exception
	{
		// 创建文件对象
		File file = new File(zipFilePath);
		// 创建本zip文件解压目录
		File unzipFile = new File(unzipDirectory);
		if (unzipFile.exists())
			unzipFile.delete();
		unzipFile.mkdir();
		unzip(file, unzipFile);
	}
 
	public static byte[] unZip(byte[] data)
	{
		byte[] b = null;
		try
		{
			ByteArrayInputStream bis = new ByteArrayInputStream(data);
			ZipInputStream zip = new ZipInputStream(bis);
			while (zip.getNextEntry() != null)
			{
				byte[] buf = new byte[1024];
				int num = -1;
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				while ((num = zip.read(buf, 0, buf.length)) != -1)
				{
					baos.write(buf, 0, num);
				}
				b = baos.toByteArray();
				baos.flush();
				baos.close();
			}
			zip.close();
			bis.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		return b;
	}

	public static byte[] gZip(byte[] data) {
		  byte[] b = null;
		  try {
		   ByteArrayOutputStream bos = new ByteArrayOutputStream();
		   GZIPOutputStream gzip = new GZIPOutputStream(bos);
		   gzip.write(data);
		   gzip.finish();
		   gzip.close();
		   b = bos.toByteArray();
		   bos.close();
		  } catch (Exception ex) {
		   ex.printStackTrace();
		  }
		  return b;
		 }
	
	
	public static void main(String[] args) throws Exception
	{
		// unzip("F:/aaaaaaaa.zip", "F:");
		// System.out.println("over....................");
		String a = "hellp aa bb cc";
		System.out.println(a.lastIndexOf("he"));

	}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

分享到:
评论

相关推荐

    Java实现压缩解压缩文件和文件夹(附源码)

    Java实现压缩解压缩文件和文件夹(附源码) zip unzip 压缩 解压缩

    php zip gzip tar..压缩解压缩类

    功能很强大,支持多种格式 使用简便.下载后解压有例子可以使用(非原创)

    zip、unzip 解压缩工具 for C++、WIN32.zip

    本资源是zip、unzip解压缩工具, 包含了压缩、解压缩的相关函数和使用范例可以方便的解压缩文件和文件夹。

    java zip解压中文乱码问题

    网上很多描述java解压中文乱码的问题,很多描述不全.由于工作需要整理出一个完整版.... new ZipUtil().unZip("E:\\aaaa\\中文.zip","E:\\aaaa\\中文","GBK"); } 实例: 将E:\\aaaa\\中文.zip解压到E:\\aaaa\\中文目录下

    java zip and unzip

    掌握java解压缩和压缩技术。

    java实现压缩与解压缩源码,demo 分享.zip

    java实现压缩与解压缩源码,demo 分享。 java实现压缩与解压缩源码。 java实现压缩与解压缩源码 java

    解压缩软件UNZIP的C源程序代码unzip

    解压缩软件UNZIP的C源程序代码unzip

    java算法,实现压缩及解压缩

    Java实现压缩与解压缩ZIP  import java.io.BufferedInputStream;  import java.io.BufferedOutputStream;  import java.io.File;  import java.io.FileInputStream;  import java.io.FileOutputStream;...

    unzip_src.zip压缩解压缩

    # 压缩文件 C:\MxDownload\unzip_src.zip 2002-12-11 10:13 3561 1328 Unzip\ReadMe.txt 2002-12-11 10:13 文件夹 文件夹 Unzip\res 2002-12-11 10:13 1078 343 Unzip\res\Unzip.ico 2002-12-11 10:13 397 172 Unzip...

    带源码的unix解压缩工具unzip5

    带源码的unix解压缩工具unzip5,可以重新用gcc编译,适合各种平台。

    VC++ MFC 如何用 CZip CUnzip类压缩解压缩文件

    VC++ MFC 如何用 CZip CUnzip类压缩解压缩文件 包括源代码和Example文件,还有说明文件~

    java压缩文件源码--ZipUtils

    // 设置压缩文件入口entry,为被读取的文件创建压缩条目 File tempFile = new File(fileArray[i].toString()); String rootStr = file.getPath(); String entryStr = null; // entry以相对路径的...

    Qt 压缩 zip/unzip

    用Qt开发的文件及文件夹得压缩以及解压功能,zip,unzip

    C++ zip压缩解压缩源代码

    C++ zip压缩解压缩源代码 压缩时调用 #include "zip.h" HZIP hz = CreateZip("c:\\simple1.zip",0); ZipAdd(hz,"znsimple.bmp", "c:\\simple.bmp"); ZipAdd(hz,"znsimple.txt", "c:\\simple.txt"); ...

    Windows命令行压缩解压缩工具

    压缩包内包含一系列的Windows平台命令行使用的压缩、解压缩工具,zip, unzip, bzip2, gzip, tar.

    linux下通过unzip直接解压zip压缩包

    unzip命令用于解压缩由zip命令压缩的“.zip”压缩包。 使用说明: a.上传unzip文件到linux b.例如将压缩文件text.zip在当前目录下解压缩: unzip test.zip c.将压缩文件text.zip在指定目录/tmp下解压缩,如果已有...

    linux/Unix 嵌入式解压unzip源码,可编译运行

    unzip解压缩代码,可以在开发板上面运行。适用于没有集成解压命令的开发板和嵌入式系统使用,unzip解压缩代码,可以在开发板上面运行。适用于没有集成解压命令的开发板和嵌入式系统使用

    批量解压缩 2005

    批量解压缩(Batch UnZip)就可以帮助您解决这个问题!!它可以帮助您自动输入密码。 通常我们解压缩一个Zip,最简单的方法就是直接在这个Zip文件上点击鼠标右键,然后选择 Extract To folder 将这个Zip文件解压到...

    java zip unzip

    java zip unzipjava zip unzipjava zip unzipjava zip unzipjava zip unzipjava zip unzipjava zip unzipjava zip unzipjava zip unzipjava zip unzipjava zip unzipjava zip unzipjava zip unzip

    java zip压缩解压工具解决中文乱码问题

    使用java压缩也解压zip文件方法,解决中文乱码问题。使用java自带的压缩解压算法,会出现中文乱码问题。使用apache io的zip包,有效解决该问题。Ant的压缩解压,也是使用该类。

Global site tag (gtag.js) - Google Analytics