Java zip 압축 풀기 파일

2231 단어
package day20;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.junit.Test;

 * zip        
public class ZipDemo {
    
     *   
    @Test
    public void test() throws Exception {
         *    
        FileOutputStream fos = new FileOutputStream("E://TestCase//day20//demo.zip");
        ZipOutputStream zout = new ZipOutputStream(fos);
        
         *     
         *      
        FileInputStream fis = new FileInputStream("E://TestCase//day20//demo.jpg");
         *     
        ZipEntry entry = new ZipEntry("picture.jpg");
         *        
        zout.putNextEntry(entry);
        
        byte[] buffer = new byte[1024];
        int len = -1;
        while((len = fis.read(buffer)) != -1) {
            zout.write(buffer, 0, len);
        }
        
         *     
        zout.closeEntry();
        fis.close();
        
         *   txt
        fis = new FileInputStream("E://TestCase//day20//fiction.txt");
        
        ZipEntry entry2 = new ZipEntry("fiction.txt");
        zout.putNextEntry(entry2);
        
        while((len = fis.read(buffer)) != -1) {
            zout.write(buffer, 0, len);
        }
        
        zout.closeEntry();
        zout.close();
        fos.close();
        fis.close();
        
        System.out.println("end");
    }
    
     *     
    @Test
    public void test5() throws Exception {
        FileInputStream fis = new FileInputStream("E://TestCase//day20//demo.zip");
        ZipInputStream zins = new ZipInputStream(fis);
        
        ZipEntry entry = null;
        
        while((entry = zins.getNextEntry()) != null) {
            FileOutputStream fos = new FileOutputStream("E://TestCase//day20//undo//" + entry.getName());
            byte[] buffer = new byte[1024];
            int len = -1;
            while((len = zins.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            zins.closeEntry();
        }
        
        zins.close();
        fis.close();
    }
}

좋은 웹페이지 즐겨찾기