java压缩流的用法_java压缩多个文件并且返回流示例

这个类可以压缩多个文件并且返回流,在程序中可以再操作返回的流做其它功能,比如验证MD5,下面看代码吧

复制代码 代码如下:

/**

* 方法描述:测试类

*/

public class TestFileStream{

//文件和压缩包存储的位置

StringtempFilePath="C:/temp/"

ListfileList=newArrayList();

fileList.add(tempFilePath+"file1.txt");

fileList.add(tempFilePath+"file2.png");

fileList.add(tempFilePath+"file3.xls");

//生成的压缩包名称

StringzipName="fileData";

//返回流

ByteArrayOutputStreamoutputStream=fileToZip(fileList,fileData,tempFilePath);

//页面输入压缩包流

byte[]buffer=outputStream.toByteArray();

//清空response

response.reset();

//设置response的Header

response.addHeader("Content-Disposition",

"attachment;filename="+

newString(("dataFile.zip").getBytes("gb2312"),"ISO8859-1"));

response.addHeader("Content-Length",""+outputStream.size());

toClient=newBufferedOutputStream(response.getOutputStream());

response.setContentType("application/octet-stream");

toClient.write(buffer);

toClient.flush();

}

/**

*方法描述:将多个文件压缩成zip包

*/

publicByteArrayOutputStreamfileToZip(ListfileList,StringzipName,StringtempFilePath){

byte[]buffer=newbyte[1024];

ZipOutputStreamout=null;

try{

out=newZipOutputStream(newFileOutputStream(tempFilePath+zipName+".zip"));

Listfiledata=newArrayList();

for(inti=0,len=fileList.size();i

{

filedata.add(newFile(fileList.get(i)));

}

for(intj=0,len=filedata.size();j

{

FileInputStreamfis=newFileInputStream(filedata.get(j));

out.putNextEntry(newZipEntry(filedata.get(j).getName()));

intdataLen;

//读入需要下载的文件的内容,打包到zip文件

while((dataLen=fis.read(buffer))>0){

out.write(buffer,0,dataLen);

}

out.closeEntry();

fis.close();

}

out.close();

}

catch(Exceptionex)

{

ex.printStackTrace();

}

//读取压缩包

Filefilezip=newFile(tempFilePath+zipName+".zip");

ByteArrayOutputStreambaos=null;

try

{

baos=newByteArrayOutputStream();

FileInputStreaminStream=newFileInputStream(filezip);

BufferedInputStreambis=newBufferedInputStream(inStream);

intc=bis.read();

while(c!=-1){

baos.write(c);

c=bis.read();

}

bis.close();

inStream.close();

}

catch(Exceptionex)

{

ex.printStackTrace();

}

returnbaos;

}

时间: 2014-03-11

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java中有两种常用的压缩: java.util.zip.GZIPOutputStreamjava.util.zip.ZipOutputStream. 它们都是继承自 java.io.OutputStream, 可以将一个字节转换为压缩写入文件. 例如,下面是一个将文本文件压缩成 gzip 格式的例子: ``` import java.io.*; import java.util.zip.*; public class GzipExample { public static void main(String[] args) throws IOException { String filePath = "example.txt"; String gzipFilePath = "example.txt.gz"; try (InputStream inputStream = new FileInputStream(filePath); OutputStream outputStream = new GZIPOutputStream(new FileOutputStream(gzipFilePath))) { byte[] buffer = new byte[1024]; int len; while ((len = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } } } } ``` 上面的代码将example.txt文件转换成了example.txt.gz文件 需要注意,在读取一个压缩时,需要使用相应的解压缩,如GZIPInputStream和ZipInputStream. ### 回答2: Java中的压缩用于压缩数据,而字节则用于处理原始的字节数据。因此,将Java压缩转换为字节可以在压缩/解压缩数据时使用原始的字节数据。 在Java中,可以使用两种压缩ZipOutputStream和GZIPOutputStreamZipOutputStream用于创建压缩包,而GZIPOutputStream用于创建.gz格式的压缩文件。 要将压缩转换为字节,可以使用ByteArrayOutputStream类。这个类是一个输出,它可以将压缩的输出保存到内存中的字节数组中。这种转换的过程如下: 1. 创建一个压缩对象,如ZipOutputStream或GZIPOutputStream。 2. 创建一个ByteArrayOutputStream对象,作为压缩的输出。 3. 将ByteArrayOutputStream对象传递给压缩的构造方法,以便将输出保存到字节数组中。 4. 使用压缩进行压缩操作。 5. 完成压缩后,可以使用toByteArray()方法从ByteArrayOutputStream对象中获取压缩后的字节数组。 要注意的是,使用字节数组保存压缩数据可能会导致内存占用较多,特别是对于大型文件或数据。因此,在使用压缩转换为字节时,需要考虑内存的使用情况,并适时释放或清空字节。 总之,Java压缩可以通过使用ByteArrayOutputStream来转换为字节,以处理原始的字节数据。这种转换可以方便地在压缩/解压缩数据时使用字节。 ### 回答3: Java中,压缩是用来将数据进行压缩的工具,而字节则是用来处理以字节为单位的数据的工具。在某些情况下,我们可能需要将压缩转换为字节来进行进一步的处理或传输。 首先,我们可以使用Java中的压缩(例如GZIPOutputStream或DeflaterOutputStream)将数据进行压缩。这些压缩可以接受字节作为输入,并将其压缩压缩(字节)。 然后,我们可以使用Java中的字节(例如ByteArrayInputStream)来读取压缩并将其转换为字节。这将允许我们以字节为单位处理压缩后的数据。 以下是一个简单的示例,演示了如何将压缩转换为字节: ```java import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.zip.GZIPOutputStream; public class CompressionExample { public static void main(String[] args) { try { // 创建一个字节数组输出,用于保存压缩后的数据 ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); // 创建一个GZIPOutputStream用于将数据压缩压缩(字节) GZIPOutputStream gzipOutput = new GZIPOutputStream(byteOutput); // 写入需要压缩的数据 gzipOutput.write("Hello, World!".getBytes()); gzipOutput.close(); // 从字节数组输出中获取压缩后的数据 byte[] compressedData = byteOutput.toByteArray(); // 使用ByteArrayInputStream将压缩转换为字节 ByteArrayInputStream byteInput = new ByteArrayInputStream(compressedData); // 读取字节中的数据 int byteRead; while ((byteRead = byteInput.read()) != -1) { // 处理每个字节的数据 // 例如,可以将每个字节写入文件或进行其他操作 System.out.print((char) byteRead); } } catch (Exception e) { e.printStackTrace(); } } } ``` 这是一个简单的示例,演示了如何将压缩转换为字节以及如何处理字节中的数据。在实际应用中,你可能需要根据具体的需求进行更详细的处理和操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值