Java程序实现压缩某目录

Java程序实现压缩某目录,第1张

public class TestZip {

public static String SERPEROT = /

public static int BUFFER =

public static void main(String args[]){

zip( e:/hello/ e:/hello zip )

}

public static void zip(String srcFile String descFile){

ZipOutputStream zos = null

FileOutputStream fos = null

File file = null

try {

fos = new FileOutputStream(descFile)

zos = new ZipOutputStream(fos)

file = new File(srcFile)

String folder = srcFile substring(srcFile lastIndexOf( / ) + srcFile length())

zip(zos file folder)

} catch (FileNotFoundException e) {

e printStackTrace()

}finally{

try{

if(zos != null){zos close()}

if(fos != null){fos close()}

}catch(Exception e){

e printStackTrace()

}

}

}

private static void zip(ZipOutputStream descFile File srcFile String srcfolder){

FileInputStream fis = null

System out println(srcFile isDirectory())

try{

if(srcFile isDirectory()){

File[] files = srcFile listFiles()

descFile putNextEntry(new ZipEntry(srcfolder + / ))//是压缩包里面的路径

srcfolder = srcfolder length() == ? : srcfolder + /

System out println(srcfolder)

for(int i= i<files lengthi++){

zip(descFile files[i] srcfolder + files[i] getName())

}

}else{

descFile putNextEntry(new ZipEntry(srcfolder))

fis = new FileInputStream(srcFile)

byte[] bytes = new byte[ ]

int n =

while((n = fis read(bytes)) != ){

descFile write(bytes n)

}

}

}catch(Exception e){

e printStackTrace()

}finally{

try{

if(fis != null){fis close()}

}catch(Exception e){

e printStackTrace()

}

}

}

lishixinzhi/Article/program/Java/hx/201311/25760

import java.io.BufferedOutputStream

import java.io.File

import java.io.FileInputStream

import java.io.FileOutputStream

import org.apache.tools.zip.ZipEntry

import org.apache.tools.zip.ZipOutputStream

/**

* @project: Test

* @author chenssy

* @date 2013-7-28

* @Description: 文件压缩工具类

*                   将指定文件/文件夹压缩成zip、rar压缩文件

*/

public class CompressedFileUtil {

  /**

   * 默认构造函数

   */

  public CompressedFileUtil(){

   

  }

  /**

   * @desc 将源文件/文件夹生成指定格式的压缩文件,格式zip

   * @param resourePath 源文件/文件夹

   * @param targetPath  目的压缩文件保存路径

   * @return void

   * @throws Exception

   */

  public void compressedFile(String resourcesPath,String targetPath) throws Exception{

      File resourcesFile = new File(resourcesPath)    //源文件

      File targetFile = new File(targetPath)          //目的

      //如果目的路径不存在,则新建

      if(!targetFile.exists()){  

          targetFile.mkdirs()

      }

   

      String targetName = resourcesFile.getName()+".zip"  //目的压缩文件名

      FileOutputStream outputStream = new FileOutputStream(targetPath+"\\"+targetName)

      ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream))

   

      createCompressedFile(out, resourcesFile, "")

   

      out.close()

  }

  /**

   * @desc 生成压缩文件。

   *                  如果是文件夹,则使用递归,进行文件遍历、压缩

   *       如果是文件,直接压缩

   * @param out  输出流

   * @param file  目标文件

   * @return void

   * @throws Exception

   */

  public void createCompressedFile(ZipOutputStream out,File file,String dir) throws Exception{

      //如果当前的是文件夹,则进行进一步处理

      if(file.isDirectory()){

          //得到文件列表信息

          File[] files = file.listFiles()

          //将文件夹添加到下一级打包目录

          out.putNextEntry(new ZipEntry(dir+"/"))

       

          dir = dir.length() == 0 ? "" : dir +"/"

       

          //循环将文件夹中的文件打包

          for(int i = 0 i <files.length i++){

              createCompressedFile(out, files[i], dir + files[i].getName())        //递归处理

          }

      }

      else{   //当前的是文件,打包处理

          //文件输入流

          FileInputStream fis = new FileInputStream(file)

       

          out.putNextEntry(new ZipEntry(dir))

          //进行写 *** 作

          int j =  0

          byte[] buffer = new byte[1024]

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

              out.write(buffer,0,j)

          }

          //关闭输入流

          fis.close()

      }

  }

  public static void main(String[] args){

      CompressedFileUtil compressedFileUtil = new CompressedFileUtil()

   

      try {

          compressedFileUtil.compressedFile("G:\\zip", "F:\\zip")

          System.out.println("压缩文件已经生成...")

      } catch (Exception e) {

          System.out.println("压缩文件生成失败...")

          e.printStackTrace()

      }

  }

}

 运行程序结果如下:

 压缩之前的文件目录结构:

如果是使用java.util下的java.util.zip进行打包处理,可能会出现中文乱码问题,这是因为java的zip方法不支持编码格式的更改,我们可以使用ant.java下的zip工具类来进行打包处理。所以需要将ant.jar导入项目的lib目录下。

上次利用java自动的java.util.zip.ZipEntry和�0�2java.util.zip.ZipFile来解压zip文件,今天发现程序在读取解压文件时居然报了空指针异常,debug程序后发现时读取不到文件,产生原先是zip压缩文件中含有中文的名称,读取文件名为乱码,

报找不到文件名,所以报了空指针,想到ant构建文件也有这个功能,换了apache的ant.jar居然解决了中文的问题。

备份下。

�0�2import java.io.BufferedReader

import java.io.File

import java.io.IOException

import java.io.InputStreamReader

import java.util.Enumeration

import org.apache.tools.zip.ZipEntry

import org.apache.tools.zip.ZipFile/*** 读取zip压缩文件中文本的内容

* @author fish*/public class ReadZip {

public static void main(String args[]) {try {String fileName = "D:/workspace/java/src/ReadZip.zip"

//构造ZipFile

ZipFile zf = new ZipFile(new File(fileName))

//返回 ZIP file entries的枚举.

Enumeration<? extends ZipEntry entries = zf.getEntries()

while (entries.hasMoreElements()) {

ZipEntry ze = entries.nextElement()

System.out.println("name:"+ze.getName())

long size = ze.getSize()

if (size 0) {

System.out.println("Length is " + size)

BufferedReader br = new BufferedReader(

new InputStreamReader(zf.getInputStream(ze)))

String line

while ((line = br.readLine()) != null) {


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/11358383.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-15
下一篇2023-05-15

发表评论

登录后才能评论

评论列表(0条)

    保存