Here you can find the source of compressDir(String dirPath, String archiveName)
Parameter | Description |
---|---|
dirPath | path to the directory |
archiveName | the name of the ZIP archive that is going to be created |
Parameter | Description |
---|---|
IOException | an exception |
public static void compressDir(String dirPath, String archiveName) throws IOException
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Reader; import java.net.URL; import java.util.UUID; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import net.sf.json.JSON; import net.sf.json.JSONObject; import org.apache.log4j.Logger; public class Main{ private static Logger l = Logger.getLogger(FileUtil.class); private static final int BUF_SIZE = 2048; /**// ww w . j a v a2 s. com * Compresses local directory to the archiveName * * @param dirPath path to the directory * @param archiveName the name of the ZIP archive that is going to be created * @throws IOException */ public static void compressDir(String dirPath, String archiveName) throws IOException { l.debug("Compressing " + dirPath + " -> " + archiveName); File d = new File(dirPath); if (d.isDirectory()) { File[] files = d.listFiles(); byte data[] = new byte[BUF_SIZE]; ZipOutputStream out = new ZipOutputStream( new BufferedOutputStream(new FileOutputStream( archiveName))); for (File file : files) { BufferedInputStream fi = new BufferedInputStream( new FileInputStream(file), BUF_SIZE); ZipEntry entry = new ZipEntry(file.getName()); out.putNextEntry(entry); int count; while ((count = fi.read(data, 0, BUF_SIZE)) != -1) { out.write(data, 0, count); } fi.close(); } out.close(); File file = new File(archiveName); } else throw new IOException( "The referenced directory isn't directory!"); l.debug("Compressed " + dirPath + " -> " + archiveName); } }