複数のファイルをzipに圧縮(zipファイル操作大全)

2010 ワード

 String[] fileNames = {"11.txt","22.txt"};    
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("tomorrow.zip"));    
   
byte[] buff = new byte[1024];    
for(int i = 0;i < fileNames.length ; i++)    
{    
    FileInputStream is = new FileInputStream(fileNames[i]);    
        
    zos.putNextEntry(new ZipEntry(fileNames[i]));        //     
    int len = 0;    
    while ((len = is.read(buff)) > 0 )    
    {    
        zos.write(buff, 0, len);    
    }    
        
    zos.closeEntry();        //     
    is.close();    
}    
zos.close();   
   2. 

 ZipFile zf  = new ZipFile(new File("tomorrow.zip"));    
for(Enumeration<ZipEntry> entrys = (Enumeration<ZipEntry>)zf.entries() ; entrys.hasMoreElements();)    
{    
    String zipEnteyName = entrys.nextElement().getName();    
    System.out.println(zipEnteyName);    
}   
 3.  ZIP 
ZipInputStream ziStream = new ZipInputStream(new FileInputStream("tomorrow.zip"));    
//     
ZipFile zFile = new ZipFile(new File("tomorrow.zip"));    
   
for (Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>)zFile.entries();entries.hasMoreElements();)    
{    
    ZipEntry entry = ziStream.getNextEntry();    
    if ( null == entry)    
    {    
        break;    
    }    
    String name = entry.getName();    
    System.out.println(name);    
    FileOutputStream os = new FileOutputStream(name);    
    int len = 0;    
    byte[] buff = new byte[1024];    
    while ((len = ziStream.read(buff)) > 0)    
    {    
        os.write(buff, 0, len);    
    }    
    ziStream.closeEntry();    
    os.close();    
}    
   
ziStream.close();