📜  Java Java类

📅  最后修改于: 2022-05-13 01:54:33.648000             🧑  作者: Mango

Java Java类

此类实现了一个输出流过滤器,用于以 ZIP 文件格式写入文件。包括对压缩和未压缩条目的支持。
构造函数:

  • ZipOutputStream(OutputStream out) :创建一个新的 ZIP 输出流。
  • ZipOutputStream(OutputStream out, Charset charset) :创建一个新的 ZIP 输出流。

方法:

  • void close() :关闭 ZIP 输出流以及被过滤的流。
    Syntax :public void close()
               throws IOException
    Overrides:
    close in class DeflaterOutputStream
    Throws:
    ZipException 
    IOException
  • void closeEntry() :关闭当前 ZIP 条目并定位流以写入下一个条目。
    Syntax :public void closeEntry()
                    throws IOException
    Throws:
    ZipException 
    IOException 
  • void finish() :在不关闭底层流的情况下完成 ZIP 输出流的内容的写入。将多个过滤器连续应用于同一输出流时使用此方法。
    Syntax :public void finish()
                throws IOException
    Overrides:
    finish in class DeflaterOutputStream
    Throws:
    ZipException
    IOException
  • void putNextEntry(ZipEntry e) :开始写入新的 ZIP 文件条目并将流定位到条目数据的开头。如果仍处于活动状态,则关闭当前条目。如果条目没有指定压缩方法,则使用默认压缩方法,如果条目没有设置修改时间,则使用当前时间。
    Syntax :public void putNextEntry(ZipEntry e)
                      throws IOException
    Parameters:
    e - the ZIP entry to be written
    Throws:
    ZipException 
    IOException
  • void setComment(String comment) :设置 ZIP 文件注释。
    Syntax :public void setComment(String comment)
    Parameters:
    comment - the comment string
    Throws:
    IllegalArgumentException
  • void setLevel(int level) :设置 DEFLATED 的后续条目的压缩级别。默认设置为 DEFAULT_COMPRESSION。
    Syntax :public void setLevel(int level)
    Parameters:
    level - the compression level (0-9)
    Throws:
    IllegalArgumentException
  • void setMethod(int method) :设置后续条目的默认压缩方法。只要没有为单个 ZIP 文件条目指定压缩方法,并且最初设置为 DEFLATED,就会使用此默认值。
    Syntax :public void setMethod(int method)
    Parameters:
    method - the default compression method
    Throws:
    IllegalArgumentException
  • void write(byte[] b, int off, int len) :将字节数组写入当前 ZIP 条目数据。此方法将阻塞,直到所有字节都被写入。
    Syntax :public void write(byte[] b,
             int off,
             int len)
    Parameters:
    b - the data to be written
    off - the start offset in the data
    len - the number of bytes that are written
    Throws:
    ZipException
    IOException

程序 :

//Java program demonstrating ZipOutputStream methods
  
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
  
class ZipOutputStreamDemo
{
    public static void main(String[] args) throws IOException 
    {
          
        FileOutputStream fos = new FileOutputStream("zipfile");
        ZipOutputStream zos = new ZipOutputStream(fos);
  
        //illustrating setMethod()
        zos.setMethod(8);
          
        //illustrating setLevel method
        zos.setLevel(5);
  
        ZipEntry ze1 = new ZipEntry("ZipEntry1");
      
        //illustrating putNextEntry method
        zos.putNextEntry(ze1);
      
        //illustrating setComment
        zos.setComment("This is my first comment");
  
        //illustrating write()
        for(int i = 0; i < 10; i++)
            zos.write(i);
      
        //illustrating write(byte b[], int off, int len)
        byte b[] = { 11, 12, 13};
        zos.write(b);
  
  
        //illustrating closeEntry()
        zos.closeEntry();
          
        //Finishes writing the contents of the ZIP output stream
        // without closing the underlying stream
        zos.finish();
          
        //closing the stream
        zos.close();
  
        FileInputStream fin = new FileInputStream("zipfile");
        ZipInputStream zin = new ZipInputStream(fin);
  
        //Reads the next ZIP file entry
        ZipEntry ze = zin.getNextEntry();
  
        //the name of the entry.
        System.out.println(ze.getName());
  
        //illustrating getMethod
        System.out.println(ze.getMethod());
  
        //Reads up to byte.length bytes of data from this input stream
        // into an array of bytes.
        byte c[] = new byte[13];
          
        if(zin.available() == 1)
            zin.read(c);
  
        System.out.print(Arrays.toString(c));
          
        //closes the current ZIP entry
        zin.closeEntry();
          
        //closing the stream
        zin.close();
  
    }
}

输出 :

ZipEntry1
8
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13]