在程序中 如何判断 rar文件是否加密了呢?
http://blog.csdn.net/wqjsir/archive/2009/06/14/4269165.aspx
这个博客里面,根据getText返回值来判定是否加密!
下面是一些对rar文件的操作。可以滤过。
[code="java"]/**
- 对文件srcfile进行先解密后解压缩,然后解压缩到目录destfile下 *
- @param srcfile
- 要解密和解压缩的文件名 如c:/目标.zip
- @param destfile
- 解压缩后的目录 如c:/abc
- @param publicKey
- 公钥 */ public void decryptUnzip(String srcfile, String destfile, String keyfile) throws Exception { // 先对文件解密 File temp = new File(UUID.randomUUID().toString() + ".zip"); temp.deleteOnExit(); decrypt(srcfile, temp.getAbsolutePath(), this.getKey(keyfile)); // 解压缩 unZip(destfile, temp.getAbsolutePath()); temp.delete(); }[/code]
[code="java"]/**
- 把文件srcFile解密后存储为destFile *
- @param srcFile
- @param destFile
- @param privateKey
- @throws Exception */ private void decrypt(String srcFile, String destFile, Key privateKey) throws Exception { SecureRandom sr = new SecureRandom(); Cipher ciphers = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec spec=new IvParameterSpec(privateKey.getEncoded()); ciphers.init(Cipher.DECRYPT_MODE,privateKey,spec,sr); FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); byte[] b = new byte[2064]; while (fis.read(b) != -1) { fos.write(ciphers.doFinal(b)); } fos.close(); fis.close(); }[/code]
[code="java"]/**
- 解压缩文件zipFile保存在directory目录下 *
- @param directory
- @param zipFile */ private static void unZip(String directory, String zipFile) { try { ZipInputStream zis = new ZipInputStream( new FileInputStream(zipFile)); File f = new File(directory); f.mkdirs(); fileUnZip(zis, f); zis.close(); } catch (Exception e) { e.printStackTrace(); } }[/code]
JUnRar是一个采用纯Java实现的rar文件压缩和解压缩的开源类库。
项目主页: http://sourceforge.net/projects/java-unrar/
文档地址:
下载地址: http://sourceforge.net/projects/java-unrar/files/
纯java解压rar,支持密码
http://code.google.com/p/java-unrar/