集册 Java实例教程 使用ZipOutputStream创建Zip文件

使用ZipOutputStream创建Zip文件

欢马劈雪     最近更新时间:2020-01-02 10:19:05

882
使用ZipOutputStream创建Zip文件
/** from 
时 代 J a v a - nowjava.com**/


import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;


public class Main {


  public static void main(String args[]) {

    try {

      String zipFile = "C:/Folder/zipdemo.zip";

      String sourceFile = "C:/Folder/sourcefile.doc";


      byte[] buffer = new byte[1024];

      FileOutputStream fout = new FileOutputStream(zipFile);/** 时代Java - nowjava.com 提 供 **/


      ZipOutputStream zout = new ZipOutputStream(fout);


      FileInputStream fin = new FileInputStream(sourceFile);


      zout.putNextEntry(new ZipEntry(sourceFile));


      int length;


      while ((length = fin.read(buffer)) > 0) {

        zout.write(buffer, 0, length);

      }


      zout.closeEntry();

      fin.close();

展开阅读全文