java中复制文件和文件夹Demo,压缩单个文件夹Demo,压缩文件和文件夹Demo
2018-01-10 15:15
477 查看
public void compressFoldersAndFiles(List<String> filesAndFoldersList, String tempfolder, String filename) throws Exception { Calendar rightNow = Calendar.getInstance(); SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd_hhmmss"); String folder = "C:\\" + fmt.format(rightNow.getTime()); // 临时存储文件夹 if (!filesAndFoldersList.isEmpty()) { for (int i = 0; i < filesAndFoldersList.size(); i++) { File file = new File(filesAndFoldersList.get(i)); if (file.isDirectory()) { String temp = folder + filesAndFoldersList.get(i).substring(2); // 去掉c盘盘符,保留下载路径 if (!copyFolder(filesAndFoldersList.get(i), temp)) { throw new Exception("filesAndFoldersList copyFolder error"); } } else if (file.isFile()) { String temp = folder + filesAndFoldersList.get(i).substring(2); // 去掉c盘盘符 int b = temp.lastIndexOf("\\"); String tempFolder = temp.substring(0, b); File Tempfile = new File(tempFolder); Tempfile.mkdirs(); if (!copyFile(filesAndFoldersList.get(i), temp)) { throw new Exception("filesAndFoldersList copyFile error"); } } else { throw new Exception("filesAndFoldersList error"); } } compressSingleFolder(folder, tempfolder, filename); File file = new File(folder); while (file.exists()) { // 删除临时文件夹 System.gc(); deleteDir(file); } } else { throw new Exception("filesAndFoldersList is empty"); } }
private static boolean copyFile(String oldPathAndFileName, String targetPathAndFileName) { boolean flag = false; try { int byteread = 0; File oldfile = new File(oldPathAndFileName); if (oldfile.exists()) { InputStream inStream = new FileInputStream(oldPathAndFileName); FileOutputStream fs = new FileOutputStream(targetPathAndFileName); byte[] buffer = new byte[1444]; while ((byteread = inStream.read(buffer)) != -1) { fs.write(buffer, 0, byteread); } inStream.close(); fs.close(); flag = true; return flag; } else { flag = false; return flag; } } catch (Exception e) { e.printStackTrace(); } return flag; } private static boolean copyFolder(String oldPath, String newPath) { boolean flag = false; try { File newFile = new File(newPath); newFile.setExecutable(false); newFile.setReadable(true, true); newFile.setWritable(true, true); newFile.mkdirs(); File a = new File(oldPath); if (a.exists()) { a.setExecutable(false); a.setReadable(true, true); a.setWritable(true, true); String[] file = a.list(); File temp = null; for (int i = 0; i < file.length; i++) { if (oldPath.endsWith(File.separator)) { temp = new File(oldPath + file[i]); } else { temp = new File(oldPath + File.separator + file[i]); } if (temp.isFile()) { FileInputStream input = new FileInputStream(temp); FileOutputStream output = new FileOutputStream( newPath + File.separator + (temp.getName()).toString()); byte[] b = new byte[1024 * 5]; int len; while ((len = input.read(b)) != -1) { output.write(b, 0, len); } output.flush(); output.close(); input.close(); } if (temp.isDirectory()) { copyFolder(oldPath + File.separator + file[i], newPath + File.separator + file[i]); } } flag = true; return flag; } else { flag = false; return flag; } } catch (Exception e) { e.printStackTrace(); } return flag; }
private static List<String> listFile = new ArrayList<String>(); public void compressSingleFolder(String oldPathAndFolderName, String tempfolder, String filename) throws Exception { FileInputStream fis = null; BufferedInputStream bis = null; FileOutputStream fos = null; ZipOutputStream zos = null; if (getFile(oldPathAndFolderName)) { try { File zipFile = new File(tempfolder + "\\" + filename + ".zip"); if (zipFile.exists()) { throw new Exception(tempfolder + " has already exists " + filename + ".zip"); } else { if (!zipFile.exists()) { zipFile.getParentFile().mkdirs(); } fos = new FileOutputStream(zipFile); zos = new ZipOutputStream(new BufferedOutputStream(fos)); byte[] bufs = new byte[1024 * 1024]; for (int i = 0; i < listFile.size(); i++) { try { ZipEntry zipEntry = new ZipEntry(listFile.get(i)); zos.putNextEntry(zipEntry); fis = new FileInputStream(listFile.get(i)); bis = new BufferedInputStream(fis, 1024 * 1024); int read = 0; while ((read = bis.read(bufs, 0, 1024 * 1024)) != -1) { zos.write(bufs, 0, read); } } catch (Exception e) { throw new Exception("downloadSingleFolder ZipEntry error"); } } listFile.clear(); } } catch (FileNotFoundException | JSONException e) { e.printStackTrace(); throw new RuntimeException( b26f e); } finally { try { if (null != bis) bis.close(); if (null != zos) zos.close(); if (null != fos) fos.close(); if (null != fis) fis.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } } else { throw new Exception("folder not exists or folder is blank"); } } private static boolean getFile(String path) { File file = new File(path); if (file.exists()) { File[] tempList = file.listFiles(); for (File f : tempList) { if (f.isFile()) { listFile.add(f.getPath()); continue; } if (f.isDirectory()) { getFile(f.getPath()); } } return true; } else { return false; } }
相关文章推荐
- Java实现zip文件压缩(单个文件、文件夹以及文件和文件夹的组合压缩)
- java单个文件复制,递归删除、复制目录下所有内容,递归创建文件夹,.7z文件解压等
- Java复制单个文件和文件夹
- 【转载】用java压缩文件夹/文件
- Java IO复制个文件夹和里面的全部文件
- java 压缩文件(文件夹)
- java 的 文件、文件夹 的建立、删除、复制以及移动等功能 操作
- 文件复制--BufferedStreamDemo1.java
- java 复制文件/文件夹
- 使用java zip 压缩单个文件
- java实现文件及文件夹压缩
- Java中实现复制文件或文件夹
- 用java压缩文件夹/文件
- ( 转载)纯Java文件操作工具,支持文件、文件夹的复制、删除、移动
- 复制一个文件夹中的所有文件和文件夹的java程序实现
- java复制单个文件代码~
- JAVA实现压缩文件或文件夹
- java实现文件和文件夹的复制
- JAVA实现压缩文件或文件夹
- java中实现复制文件和文件夹