您好,登錄后才能下訂單哦!
本篇內容介紹了“Java怎么把文件夾打成壓縮包并導出”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
@Controller public class AdminController { private String filePath = AdminController.class.getResource("/").getPath().split("WEB-INF")[0]+ "upload/"; @RequestMapping(value = "export_zip.htm", method = {RequestMethod.GET, RequestMethod.POST }) public void zipwordDownAction(HttpServletRequest request,HttpServletResponse response) throws Exception { //打包文件的存放路徑 ZipCompressorByAnt zc = new ZipCompressorByAnt(filePath+ "/file.zip"); //需要打包的文件路徑 zc.compress(filePath+ "/file/"); String contentType = "application/octet-stream"; try { //導出壓縮包 download(request, response, "upload/file.zip", contentType,encodeChineseDownloadFileName(request, "file.zip")); } catch (Exception e) { request.getSession().setAttribute("msg", "暫無內容"); } //如果原壓縮包存在,則刪除 File file=new File(filePath+ "/file.zip"); if(file.exists()){ file.delete(); } } /** * 下載文件 */ public static void download(HttpServletRequest request,HttpServletResponse response, String storeName, String contentType,String realName) throws Exception { response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); BufferedInputStream bis = null; BufferedOutputStream bos = null; String ctxPath =FileUtil.class.getResource("/").getPath().split("WEB-INF")[0]; String downLoadPath = ctxPath + storeName; long fileLength = new File(downLoadPath).length(); response.setContentType(contentType); response.setHeader("Content-disposition", "attachment; filename=" + new String(realName.getBytes("utf-8"), "ISO8859-1")); response.setHeader("Content-Length", String.valueOf(fileLength)); bis = new BufferedInputStream(new FileInputStream(downLoadPath)); bos = new BufferedOutputStream(response.getOutputStream()); byte[] buff = new byte[2048]; int bytesRead; while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } bis.close(); bos.close(); } /** * 對文件流輸出下載的中文文件名進行編碼 屏蔽各種瀏覽器版本的差異性 */ public static String encodeChineseDownloadFileName(HttpServletRequest request, String pFileName) throws UnsupportedEncodingException { String filename = null; String agent = request.getHeader("USER-AGENT"); if (null != agent){ if (-1 != agent.indexOf("Firefox")) {//Firefox filename = "=?UTF-8?B?" + (new String(org.apache.commons.codec.binary.Base64.encodeBase64(pFileName.getBytes("UTF-8"))))+ "?="; }else if (-1 != agent.indexOf("Chrome")) {//Chrome filename = new String(pFileName.getBytes(), "ISO8859-1"); } else {//IE7+ filename = java.net.URLEncoder.encode(pFileName, "UTF-8"); filename = StringUtils.replace(filename, "+", "%20");//替換空格 } } else { filename = pFileName; } return filename; }
import java.io.File; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.Zip; import org.apache.tools.ant.types.FileSet; public class ZipCompressorByAnt { private File zipFile; public ZipCompressorByAnt(String pathName) { zipFile = new File(pathName); } public void compress(String srcPathName) { File srcdir = new File(srcPathName); if (!srcdir.exists()) throw new RuntimeException(srcPathName + "不存在!"); Project prj = new Project(); Zip zip = new Zip(); zip.setProject(prj); zip.setDestFile(zipFile); FileSet fileSet = new FileSet(); fileSet.setProject(prj); fileSet.setDir(srcdir); //fileSet.setIncludes("**/*.java"); 包括哪些文件或文件夾 eg:zip.setIncludes("*.java"); //fileSet.setExcludes(...); 排除哪些文件或文件夾 zip.addFileset(fileSet); zip.execute(); } }
關于Java下載zip文件并導出的方法,瀏覽器導出。
String downloadName = "下載文件名稱.zip"; downloadName = BrowserCharCodeUtils.browserCharCodeFun(request, downloadName);//下載文件名亂碼問題解決 //將文件進行打包下載 try { OutputStream out = response.getOutputStream(); byte[] data = createZip("/fileStorage/download");//服務器存儲地址 response.reset(); response.setHeader("Content-Disposition","attachment;fileName="+downloadName); response.addHeader("Content-Length", ""+data.length); response.setContentType("application/octet-stream;charset=UTF-8"); IOUtils.write(data, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); }
public byte[] createZip(String srcSource) throws Exception{ ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(outputStream); //將目標文件打包成zip導出 File file = new File(srcSource); a(zip,file,""); IOUtils.closeQuietly(zip); return outputStream.toByteArray(); }
public void a(ZipOutputStream zip, File file, String dir) throws Exception { //如果當前的是文件夾,則進行進一步處理 if (file.isDirectory()) { //得到文件列表信息 File[] files = file.listFiles(); //將文件夾添加到下一級打包目錄 zip.putNextEntry(new ZipEntry(dir + "/")); dir = dir.length() == 0 ? "" : dir + "/"; //循環將文件夾中的文件打包 for (int i = 0; i < files.length; i++) { a(zip, files[i], dir + files[i].getName()); //遞歸處理 } } else { //當前的是文件,打包處理 //文件輸入流 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); ZipEntry entry = new ZipEntry(dir); zip.putNextEntry(entry); zip.write(FileUtils.readFileToByteArray(file)); IOUtils.closeQuietly(bis); zip.flush(); zip.closeEntry(); } }
“Java怎么把文件夾打成壓縮包并導出”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。