1、複製檔案夾
private void copyFile(File sourceFile,File targetFile){try {if(!sourceFile.exists()){return;}else{if(sourceFile.isDirectory()){if(!targetFile.exists()){targetFile.mkdirs();}File[] list=sourceFile.listFiles();for(File f:list){File newFile=new File(targetFile,f.getName());copyFile(f, newFile);}}else{if(!targetFile.exists()){targetFile.createNewFile();}FileInputStream input = new FileInputStream(sourceFile);BufferedInputStream inBuff = new BufferedInputStream(input); // 建立檔案輸出資料流並對它進行緩衝FileOutputStream output = new FileOutputStream(targetFile);BufferedOutputStream outBuff = new BufferedOutputStream(output); // 緩衝數組byte[] b = new byte[1024 * 5];int len; // 重新整理此緩衝的輸出資料流while ((len = inBuff.read(b)) != -1) {outBuff.write(b, 0, len);}outBuff.flush(); // 關閉流inBuff.close();outBuff.close();output.close();input.close();}}} catch (Exception e) {Tools.errorList(e, "複製檔案");}}
2、刪除檔案夾
private void deleteFile(File file) {if(file.exists()){if(file.isFile()){file.delete();}else if (file.isDirectory()) {File files[] = file.listFiles(); //聲明目錄下所有的檔案 files[];for(int i=0;i<files.length;i++){ //遍曆目錄下所有的檔案deleteFile(files[i]); //把每個檔案 用這個方法進行迭代 } }file.delete();}}
3、壓縮檔夾
File XMFile=new File(filePath+"fileName"); File[] fileList = XMFile.listFiles();ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(filePath+"<span style="font-family: Arial, Helvetica, sans-serif;">fileName.</span>zip"));if (fileList.length!=0) {for (int i = 0; i < fileList.length; i++) {ZIP.zipFile(zos, fileList[i], "");}}zos.close();
ZIP類中的方法:
<pre name="code" class="html">public static void zipFile(ZipOutputStream output, File file, String basePath) throws Exception { FileInputStream fis = null; BufferedInputStream bis = null; try { if (file.isDirectory()) {// 檔案為目錄 File list[] = file.listFiles(); basePath = basePath + (basePath.length() == 0 ? "" : "/") + file.getName(); for (File f : list) { zipFile(output, f, basePath); } } else {// 壓縮檔 basePath = (basePath.length() == 0 ? "" : basePath + "/") + file.getName(); output.putNextEntry(new ZipEntry(basePath)); fis = new FileInputStream(file); bis = new BufferedInputStream(fis); byte[] buf = new byte[1024]; int len; while ((len = bis.read(buf)) != -1) { output.write(buf, 0, len); } output.flush(); } } catch (Exception ex) { throw ex; } finally { if (bis != null) { bis.close(); } if (fis != null) { fis.close(); } } }
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">4、解壓zip檔案</span>
public static void upZIP(File unZipFile,File unZipFolder) throws Exception{ InputStream input = null; OutputStream output = null; ZipFile zipFile = null; try {zipFile = new ZipFile(unZipFile); // 建立zip檔案對象Enumeration zipEnum = zipFile.getEntries(); // 得到zip檔案條目枚舉對象 ZipEntry entry = null; // 定義對象 String entryName = null, path = null; String names[] = null; int length; while (zipEnum.hasMoreElements()) { // 迴圈讀取條目 entry = (ZipEntry) zipEnum.nextElement(); // 得到當前條目 entryName = new String(entry.getName()); // 用/分隔條目名稱 names = entryName.split("\\/"); length = names.length; path = unZipFolder.getAbsolutePath(); for (int v = 0; v < length; v++) { if (v < length - 1){ // 最後一個目錄之前的目錄 File tempFile=new File(path += "/" + names[v] + "/"); if(!tempFile.exists()){ tempFile.mkdirs(); } }else { // 最後一個 if (entryName.endsWith("/")){ // 為目錄,則建立檔案夾 File tempFile=new File(unZipFolder.getAbsolutePath()+ "/" + entryName); if(!tempFile.exists()){ tempFile.mkdirs(); } }else { // 為檔案,則輸出到檔案 input = zipFile.getInputStream(entry); output = new FileOutputStream(new File(unZipFolder.getAbsolutePath()+ "/" + entryName)); byte[] buffer = new byte[1024 * 8]; int readLen = 0; while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1) { output.write(buffer, 0, readLen); } if (input != null) input.close(); if (output != null) { output.flush(); output.close(); } } } } } } catch (Exception ex) { throw ex; } finally { try { if (input != null) input.close(); if (output != null) { output.flush(); output.close(); } if(zipFile!=null){zipFile.close(); }} catch (Exception e) {} } }
5、把檔案寫成二進位流字串
public static String ZIPtoSTR(File file) throws Exception{ StringBuffer suf = new StringBuffer(); FileInputStream fis = null; int temp = 0; fis = new FileInputStream(file); while((temp = fis.read()) != -1){ <span style="white-space:pre"></span>suf.append(temp); <span style="white-space:pre"></span>suf.append(","); <span style="white-space:pre"></span> } <span style="white-space:pre"></span>fis.close();return suf.toString(); }
6、把二進位流轉換為檔案
public static File STRtoZIP(String str,File file) throws Exception{ FileOutputStream fos = new FileOutputStream(file); String[] str_arr = str.split(","); for(String v:str_arr){fos.write(Integer.valueOf(v)); } fos.close(); return file; }