?? ziptoolkit.java
字號(hào):
buffer.append(st.nextToken()); } deleteFile(new File(buffer.toString())); File[] files = file.listFiles(); if (files != null && files.length > 0) { rewriteFile(files); } else { newFile(fileName); } deleteFile(file); } /** * 重命名 * * @param entry 待重命名文件或者目錄所對(duì)應(yīng)的實(shí)體 * @param name 新的名稱 */ public void rename(ZipEntry entry, String name) { File file = new File(WORKING_DIR); extractFile(file, entries.toArray(), null); StringBuffer buffer = new StringBuffer(); buffer.append(file.getAbsolutePath()); String last = ""; StringTokenizer st = new StringTokenizer(entry.getName(), DELIMITER1); while (st.hasMoreTokens()) { last = st.nextToken(); buffer.append(DELIMITER2); buffer.append(last); } String newName = buffer.substring(0, buffer.length() - last.length()); (new File(buffer.toString())).renameTo(new File(newName + name)); rewriteFile(file.listFiles()); deleteFile(file); } /** * 提取文件 * * @param file 待提取文件的存放目錄 * @param objs 待提取文件的實(shí)體列表 * @param omit 待提取文件的相對(duì)路徑 */ public void extractFile(File file, Object[] objs, String omit) { byte data[] = new byte[BUFFER_SIZE]; try { ZipFile zipFile = new ZipFile(fileName); for (int i = 0; i < objs.length; i++) { ZipEntry zipEntry = zipFile.getEntry(objs[i].toString()); String name = zipEntry.getName(); if (omit != null) { name = name.substring(omit.length(), name.length()); } File file2 = new File(file, (new File(name)).getPath()); if (zipEntry.isDirectory() == true) { file2.mkdirs(); } else { file2.getParentFile().mkdirs(); BufferedInputStream bis = new BufferedInputStream( zipFile.getInputStream(zipEntry), BUFFER_SIZE); BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(file2), BUFFER_SIZE); int count; while ((count = bis.read(data, 0 , BUFFER_SIZE)) != -1) { bos.write(data, 0, count); } bos.close(); bis.close(); } } zipFile.close(); } catch (IOException e) { e.printStackTrace(); } } /*------------------------------------------------------------------------* * 私有方法 * *------------------------------------------------------------------------*/ /** * 新建文件 * * @param fileName 待新建文件的文件名 */ private void newFile(String fileName) { entries.clear(); this.fileName = fileName; try { new FileOutputStream(fileName); } catch (IOException e) { e.printStackTrace(); } } /** * 復(fù)制文件 * * @param src 源文件 * @param dest 目標(biāo)文件 */ private void copyFile(File src, File dest) { byte data[] = new byte[BUFFER_SIZE]; try { InputStream fis = new FileInputStream(src); InputStream bis = new BufferedInputStream(fis, BUFFER_SIZE); OutputStream fos = new FileOutputStream(dest); OutputStream bos = new BufferedOutputStream(fos, BUFFER_SIZE); int count; while ((count = bis.read(data, 0 , BUFFER_SIZE)) != -1) { bos.write(data, 0, count); } bos.close(); fos.close(); bis.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 刪除文件 * * @param file 待刪除的文件,如果是目錄則刪除整個(gè)目錄 */ private void deleteFile(File file) { if (file.isFile()) { file.delete(); } else if (file.isDirectory()) { File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { deleteFile(files[i]); } file.delete(); } } /** * 重寫文件 * * @param files 重新寫入Zip文件中的文件列表 */ private void rewriteFile(File[] files) { entries.clear(); (new File(fileName)).delete(); parentDir = files[0].getParent(); try { OutputStream fos = new FileOutputStream(fileName, true); OutputStream bos = new BufferedOutputStream(fos); ZipOutputStream zos = new ZipOutputStream(bos); for (int i = 0; i < files.length; i++) { if (files[i].isFile() == true) { zipFile(files[i], new File(""), zos); } else if (files[i].isDirectory() == true) { try { ZipEntry entry = new ZipEntry(files[i].getName() + DELIMITER1); zos.putNextEntry(entry); zos.closeEntry(); entries.add(entry); } catch (ZipException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } recursiveZip(files[i], new File(files[i].getName()), zos); } } zos.close(); bos.close(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 壓縮文件 * * @param file 待壓縮的文件 * @param currentDir 當(dāng)前目錄 * @param zos 指定的ZipOutputStream,用于將文件壓縮后的數(shù)據(jù)寫入其中 */ private void zipFile(File file, File currentDir, ZipOutputStream zos) { try { String relevantPath = ""; InputStream fis = new FileInputStream(file); InputStream bis = new BufferedInputStream(fis, BUFFER_SIZE); if (currentDir.getPath().length() == 0) { relevantPath = file.getName(); } else { try { StringBuffer buffer = new StringBuffer(); buffer.append("file:"); buffer.append(currentDir.getPath()); buffer.append(file.separator); buffer.append(file.getName()); relevantPath = (new URL(buffer.toString())).getPath(); } catch (MalformedURLException e) { e.printStackTrace(); } } ZipEntry entry = new ZipEntry(relevantPath); File orginal = new File(parentDir, relevantPath); entry.setSize(orginal.length()); zos.putNextEntry(entry); entries.add(entry); int count; byte data[] = new byte[BUFFER_SIZE]; while ((count = bis.read(data, 0 , BUFFER_SIZE)) != -1) { zos.write(data, 0, count); } bis.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 遞歸壓縮文件 * * @param file 待壓縮的文件 * @param currentDir 當(dāng)前目錄 * @param zos 指定的ZipOutputStream,用于將文件壓縮后寫入其中 */ private void recursiveZip(File file, File currentDir, ZipOutputStream zos) { String relevantPath = ""; for (int i = 0; i < file.list().length; i++) { File fileToBeZipped = file.listFiles()[i]; if (fileToBeZipped.isFile() == true) { zipFile(fileToBeZipped, currentDir, zos); } else if (fileToBeZipped.isDirectory() == true) { try { try { StringBuffer buffer = new StringBuffer(); buffer.append("file:"); buffer.append(currentDir.getPath()); buffer.append(file.separator); buffer.append(fileToBeZipped.getName()); buffer.append(DELIMITER1); relevantPath = (new URL(buffer.toString())).getPath(); } catch (MalformedURLException e) { e.printStackTrace(); } ZipEntry entry = new ZipEntry(relevantPath); zos.putNextEntry(entry); zos.closeEntry(); entries.add(entry); } catch (ZipException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } StringBuffer buffer = new StringBuffer(); buffer.append(currentDir.getPath()); buffer.append(file.separator); buffer.append(fileToBeZipped.getName()); recursiveZip(fileToBeZipped, new File(buffer.toString()), zos); } } }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -