Java 使用zip类解压网络上接收的压缩数据流

Tangnelson 2017-01-22 08:56:11
如题,在写一个手机客户端,从服务器上下载配置文件。但服务器上的文件先被zip压缩了,且传过来的是数据流。网上看到的都是在压缩解压文件,本人新手,不知道如何处理。
...全文
733 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tangnelson 2017-02-19
  • 打赏
  • 举报
回复
过了这么久,还是又遇到这个问题了,还是自己解决了。不同时候,对待这个问题真的好像不一样,对Java了解多了一点吧。

/*在这个方法里面进行解压*/
    public static byte[] releaseCompression(byte[] bytesArray) throws IOException{
        
        ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(bytesArray));

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        
        ZipEntry ze  = null;
        
        while((ze = zis.getNextEntry()) != null){
        		
            int fileSize = (int) ze.getSize();
                
            byte[] b = new byte[fileSize];    
            int rb = 0, chunk = 0;
            
            //通过循环一次把数据全部都读到内存中去
            while(fileSize - rb > 0)
            {   
                chunk = zis.read(b, rb, fileSize - rb);
                if (chunk <= 0) 
                {
                    break;
                }
                rb += chunk;
            }
            
            bos.write(b);
            zis.close();
            break;
        }
        
        return bos.toByteArray();
    }
上面函数就是将内存已经被压缩的数据解压成原始数据。再给个demo,有需要的可以试试,我简单试了试,是OK的。

public class ZipBufferDemo {

	public static void main(String[] args) throws IOException{

		//先读一个ZiP压缩文件进来,变成把它读到内存里去
		FileInputStream fis = new FileInputStream("in.zip");
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		int c = 0;
		byte [] buf = new byte[1024]; 
		while((c=fis.read(buf)) != -1){
			bos.write(buf, 0, c);
		}
		buf = bos.toByteArray();
		
		//在调用下面的内存压缩与解压缩方法
		buf = ZipBufferDemo.releaseCompression(buf);
		
		//把得到的解压后的字节再写入到结果文件中去
		FileOutputStream fos = new FileOutputStream("out.txt");
		fos.write(buf);
		
		fos.close();
		fis.close();
	}
}
Tangnelson 2017-01-22
  • 打赏
  • 举报
回复
最后用了“曲线救国”的方法,先保存成文件,再去解压文件。
Tangnelson 2017-01-22
  • 打赏
  • 举报
回复
我想使用网上压缩文件的代码,比如下面这个。
/** 
     * 解压缩zip包 
     * @param zipFilePath zip文件的全路径 
     * @param unzipFilePath 解压后的文件保存的路径 
     * @param includeZipFileName 解压后的文件保存的路径是否包含压缩文件的文件名。true-包含;false-不包含 
     */  
    @SuppressWarnings("unchecked")  
    public static void unzip(String zipFilePath, String unzipFilePath, boolean includeZipFileName) throws Exception  
    {  
        if (StringUtils.isEmpty(zipFilePath) || StringUtils.isEmpty(unzipFilePath))  
        {  
            throw new ParameterException(ICommonResultCode.PARAMETER_IS_NULL);            
        }  
        File zipFile = new File(zipFilePath);  
        //如果解压后的文件保存路径包含压缩文件的文件名,则追加该文件名到解压路径  
        if (includeZipFileName)  
        {  
            String fileName = zipFile.getName();  
            if (StringUtils.isNotEmpty(fileName))  
            {  
                fileName = fileName.substring(0, fileName.lastIndexOf("."));  
            }  
            unzipFilePath = unzipFilePath + File.separator + fileName;  
        }  
        //创建解压缩文件保存的路径  
        File unzipFileDir = new File(unzipFilePath);  
        if (!unzipFileDir.exists() || !unzipFileDir.isDirectory())  
        {  
            unzipFileDir.mkdirs();  
        }  
          
        //开始解压  
        ZipEntry entry = null;  
        String entryFilePath = null, entryDirPath = null;  
        File entryFile = null, entryDir = null;  
        int index = 0, count = 0, bufferSize = 1024;  
        byte[] buffer = new byte[bufferSize];  
        BufferedInputStream bis = null;  
        BufferedOutputStream bos = null;  
        ZipFile zip = new ZipFile(zipFile);  
        Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>)zip.entries();  
        //循环对压缩包里的每一个文件进行解压       
        while(entries.hasMoreElements())  
        {  
            entry = entries.nextElement();  
            //构建压缩包中一个文件解压后保存的文件全路径  
            entryFilePath = unzipFilePath + File.separator + entry.getName();  
            //构建解压后保存的文件夹路径  
            index = entryFilePath.lastIndexOf(File.separator);  
            if (index != -1)  
            {  
                entryDirPath = entryFilePath.substring(0, index);  
            }  
            else  
            {  
                entryDirPath = "";  
            }             
            entryDir = new File(entryDirPath);  
            //如果文件夹路径不存在,则创建文件夹  
            if (!entryDir.exists() || !entryDir.isDirectory())  
            {  
                entryDir.mkdirs();  
            }  
              
            //创建解压文件  
            entryFile = new File(entryFilePath);  
            if (entryFile.exists())  
            {  
                //检测文件是否允许删除,如果不允许删除,将会抛出SecurityException  
                SecurityManager securityManager = new SecurityManager();  
                securityManager.checkDelete(entryFilePath);  
                //删除已存在的目标文件  
                entryFile.delete();   
            }  
              
            //写入文件  
            bos = new BufferedOutputStream(new FileOutputStream(entryFile));  
            bis = new BufferedInputStream(zip.getInputStream(entry));  
            while ((count = bis.read(buffer, 0, bufferSize)) != -1)  
            {  
                bos.write(buffer, 0, count);  
            }  
            bos.flush();  
            bos.close();              
        }  
    }  
从开始解压的关键代码那,这一句:ZipFile zip = new ZipFile(zipFile); 创建一个ZipFile对象,我看了ZipFile的构造函数,不是要求传file做参数,就是要name(String)做参数。也就是说我改不了,不能把构造参数换成字节流。

51,410

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧