} /** * 判断压缩目标是“目录(文件夹)”、“文件” * @param file * @param out * @param basedir */ private void compressByType(String srcPathName,ZipOutputStream out,String basedir,String key){ File file = new File(srcPathName); if(file.isDirectory()){ //目录 logger.info(\压缩\ this.compressDirectory(file, out, basedir,key); }else{ logger.info(\压缩\ this.compressFile(srcPathName, out, basedir,key); } } /** * 压缩目录 * @param dir * @param out * @param basedir */ private void compressDirectory(File dir, ZipOutputStream out, String basedir,String key) { if(!dir.exists()){ return; } File[] files = dir.listFiles(); for(int i = 0; i private void compressFile(String srcPathName, ZipOutputStream out, String basedir,String key) { File file = new File(srcPathName); if(!file.exists()){ return; } try{ //对文件进行加密 String path = file.getPath(); int index = path.lastIndexOf(\ String destFile = path.substring(0, index)+\ File dest = new File(destFile); InputStream in = new FileInputStream(srcPathName); OutputStream fileout = new FileOutputStream(destFile); //创建一个长度为1024的内存空间,1024足够大,可以一次将文件读取完,不会出现中文注释乱码问题 byte[] buffer = new byte[1024]; int r; byte[] buffer2 = new byte[1024]; //使用循环来重复读取数据 // 读入多个字节到字节数组buffer中,r为一次读入的字节数 while((r = in.read(buffer))>0){ for(int i = 0;i while((count =bis.read(data,0,BUFFER))!=-1){ //bis.read()returns the number of bytes actually read. out.write(data,0,count);//Writes an array of bytes to the current ZIP entry data. out.flush(); } bis.close(); }catch(Exception e){ throw new RuntimeException(e); } } /** * 添加密匙 * @param fileName文件路径 * @param key 密匙 */ public static void appendLock(String fileName,String key){ try{ //打开一个随机访问文件流,按读写方式 RandomAccessFile randomFile = new RandomAccessFile(fileName, \ //文件长度,字节数 //long fileLength = randomFile.length(); //将写文件指针移到文件20的位置,写入密匙 int keyLength = key.length(); /*int avgLength = keyLength/3; int start = 20; int center = 40; int last = 60; randomFile.seek(start); randomFile.writeBytes(key.substring(0,avgLength)); randomFile.seek(center); randomFile.writeBytes(key.substring(avgLength,2*avgLength)); randomFile.seek(last); randomFile.writeBytes(key.substring(2*avgLength,keyLength));*/ byte[] b = key.getBytes(); randomFile.setLength(randomFile.length() + b.length); for(long i = randomFile.length() - 1; i > b.length + 20 - 1; i--){ randomFile.seek(i - b.length); byte temp = randomFile.readByte(); randomFile.seek(i); randomFile.writeByte(temp); } randomFile.seek(20); randomFile.write(b); randomFile.close(); }catch(IOException e){ e.printStackTrace(); } } /** * 解压文件到指定目录 * @param zipFile 源zip路径 * @param descDir 输出路径 * @author */ public static void unZipFiles(String zipFile,String descDir,String key)throws IOException{ long startTime=System.currentTimeMillis(); try { ZipInputStream Zin=new ZipInputStream(new FileInputStream(zipFile)); //输入源zip路径 BufferedInputStream Bin=new BufferedInputStream(Zin); String Parent=descDir;//输出路径(文件夹目录) File Fout=null; ZipEntry entry; try { while((entry = Zin.getNextEntry())!=null && !entry.isDirectory()){ Fout=new File(Parent,entry.getName()); if(!Fout.exists()){ (new File(Fout.getParent())).mkdirs(); } FileOutputStream out=new FileOutputStream(Fout); BufferedOutputStream Bout=new BufferedOutputStream(out); int b; while((b=Bin.read())!=-1){ Bout.write(b); } Bout.close(); out.close(); System.out.println(Fout+\解压成功\ try { String decryptFilePath = Fout.getPath().replace(\ decrypt(decryptFilePath,key); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } Bin.close(); Zin.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } long endTime=System.currentTimeMillis(); System.out.println(\耗费时间: \ } /** * 解密 * @param srcPathName 源文件 * @param tempUrl 临时文件 * @param ketLength 密码长度 * @return * @throws Exception */ public static String decrypt(String srcPathName, String key) throws Exception{ //将解密秘钥进行md5加密 String md5pass=new MD5().encrypt(key); System.out.println(md5pass); if(matchLock(srcPathName, md5pass)){// //匹配成功开始解密 File file = new File(srcPathName); if (!file.exists()) { return null; } int keyLength = md5pass.length(); int index = srcPathName.lastIndexOf(\ String destFile = srcPathName.substring(0, index)+\ File dest = new File(destFile); if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); } InputStream is = new FileInputStream(srcPathName); OutputStream out = new FileOutputStream(destFile); byte[] buffer = new byte[1024]; byte[] buffer2=new byte[1024]; 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库java文件加密压缩及解密-流程图及源码(3)在线全文阅读。
相关推荐: