import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.security.Key; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import java.security.SecureRandom; import java.util.Enumeration; import java.util.UUID; import javax.crypto.Cipher; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; import org.apache.tools.zip.ZipOutputStream; public class ZIPOperater { static final int BUFFER = 1024; private static PrivateKey privateKey; private static PublicKey publicKey; public void zip(String inputFile, String zipFile) { File f = new File(inputFile); try { ZipOutputStream out = new ZipOutputStream(new FileOutputStream( zipFile)); out.setEncoding("UTF-8"); if (f.isDirectory()) { zip(out, f, ""); } else if (f.isFile()) { createEntry(out, f, f.getName()); } System.out.println("compress finish "); out.close(); } catch (IOException e) { e.printStackTrace(); } } public void createEntry(ZipOutputStream out, File f, String base) throws IOException { out.putNextEntry(new ZipEntry(base)); FileInputStream in = new FileInputStream(f); System.out.println("compress file " + f.getCanonicalPath()); int len; byte[] buf = new byte[BUFFER]; while ((len = in.read(buf, 0, BUFFER)) != -1) { out.write(buf, 0, len); } in.close(); } public void zip(ZipOutputStream out, File f, String base) throws IOException { if (f.isDirectory()) { System.out.println("unfold :" + f.getCanonicalPath()); File[] fl = f.listFiles(); base = base.length() == 0 ? "" : base + "//"; for (int i = 0; i < fl.length; i++) { zip(out, fl[i], base + fl[i].getName()); } } else { createEntry(out, f, base); } } public void unzip(String fileName, String filePath) throws IOException { ZipFile zipFile = new ZipFile(fileName, "UTF-8"); Enumeration emnu = zipFile.getEntries(); while (emnu.hasMoreElements()) { ZipEntry entry = (ZipEntry) emnu.nextElement(); if (entry.isDirectory()) { File nfile = new File(filePath + entry.getName()); nfile.mkdirs(); System.out.println(nfile.getCanonicalPath()); continue; } BufferedInputStream bis = new BufferedInputStream(zipFile .getInputStream(entry)); File file = new File(filePath + System.getProperty("file.separator") + entry.getName()); File parent = file.getParentFile(); if (parent != null && (!parent.exists())) { parent.mkdirs(); } FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER); byte[] buf = new byte[BUFFER]; int len = 0; while ((len = bis.read(buf, 0, BUFFER)) != -1) { fos.write(buf, 0, len); } bos.flush(); bos.close(); bis.close(); System.out.println("uncompress file:" + file.getCanonicalPath()); } zipFile.close(); System.out.println("uncompress finish!"); } public static Key getKey(String keyPath) throws Exception { Key key = null; FileInputStream fis = new FileInputStream(keyPath); ObjectInputStream ofs = new ObjectInputStream(fis); key = (Key) ofs.readObject(); return key; } private void encrypt(String srcFile, String destFile, Key privateKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, privateKey); FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); byte[] b = new byte[53]; while (fis.read(b) != -1) { fos.write(cipher.doFinal(b)); } fos.close(); fis.close(); } private static void decrypt(String srcFile, String destFile, Key privateKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); byte[] b = new byte[64]; while (fis.read(b) != -1) { fos.write(cipher.doFinal(b)); } fos.close(); fis.close(); } public void encryptZip(String srcFile, String destFile, String keyfile) throws Exception { SecureRandom sr = new SecureRandom(); KeyPairGenerator kg = KeyPairGenerator.getInstance("RSA"); kg.initialize(512, sr); KeyPair kp = kg.generateKeyPair(); ZIPOperater.privateKey = kp.getPrivate(); ZIPOperater.publicKey = kp.getPublic(); File f = new File(keyfile); f.createNewFile(); FileOutputStream fos = new FileOutputStream(f); ObjectOutputStream dos = new ObjectOutputStream(fos); dos.writeObject(ZIPOperater.publicKey); dos.close(); fos.close(); File temp = new File(UUID.randomUUID().toString() + ".zip"); temp.deleteOnExit(); zip(srcFile, temp.getAbsolutePath()); encrypt(temp.getAbsolutePath(), destFile, privateKey); temp.delete(); } public void decryptUnzip(String srcfile, String destfile, String keyPath) throws Exception { File temp = new File(UUID.randomUUID().toString() + ".zip"); temp.deleteOnExit(); Key key = getKey(keyPath); decrypt(srcfile, temp.getAbsolutePath(), key); unzip(temp.getAbsolutePath(), destfile); temp.delete(); } public static void main(String[] args) throws Exception { ZIPOperater zop = new ZIPOperater(); long time = System.currentTimeMillis(); zop.encryptZip("E://src", "e://zip//doc.zip", "e://zip/public.key"); System.out.println("compress and encrypt cost time: :" + (System.currentTimeMillis() - time)); time = System.currentTimeMillis(); zop.decryptUnzip("e://zip//doc.zip", "E://src2", "e://zip/public.key"); System.out.println("decrypt and uncompress cost time:" + (System.currentTimeMillis() - time)); } }