여러개의 파일을 압축된 형태로 저장할때 사용함..
---------------------
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipTest {
private static final int COMPRESSION_LEVEL = 8;
private static void zipEntry(String strZipEntryName,
byte[] buffer, ZipOutputStream zipOutputStream)
throws Exception {
try {
ZipEntry zentry = new ZipEntry(strZipEntryName);
zipOutputStream.putNextEntry(zentry);
int cnt = buffer.length;
zipOutputStream.write(buffer, 0, cnt);
zipOutputStream.closeEntry();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
public static void zip(String fileName)
throws IOException {
if (fileName.substring(fileName.lastIndexOf(".")).equalsIgnoreCase(".zip")==false)
return;
BufferedOutputStream outputStream = null;
ZipOutputStream zipOutputStream = null;
try {
outputStream = new BufferedOutputStream(new FileOutputStream(fileName));
zipOutputStream = new ZipOutputStream(outputStream);
zipOutputStream.setLevel(COMPRESSION_LEVEL);
String innterfilename = null;
byte[] data = null;
// Test code
innterfilename = "1/0/0/0/a.dat";
data = "aa".getBytes();
zipEntry(innterfilename, data, zipOutputStream);
innterfilename = "1/0/0/1/b.dat";
data = "bb".getBytes();
zipEntry(innterfilename, data, zipOutputStream);
// end of test code
zipOutputStream.finish();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (zipOutputStream != null) {
zipOutputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
ZipTest.zip("C:/Test.zip");
}
}