1. 解析zip文件读取文件流:
package test; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipInputStream; public class aaaa { public static void main(String[] args) throws Exception { try { readZipFile("D:\\ztree.zip"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void readZipFile(String file) throws Exception { ZipFile zf = new ZipFile(file); InputStream in = new BufferedInputStream(new FileInputStream(file)); ZipInputStream zin = new ZipInputStream(in); ZipEntry ze; while ((ze = zin.getNextEntry()) != null) { if (ze.isDirectory()) { } else { System.err.println("file - " + ze.getName() + " : " + ze.getSize() + " bytes"); long size = ze.getSize(); if (size > 0) { BufferedReader br = new BufferedReader( new InputStreamReader(zf.getInputStream(ze))); String line; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); } System.out.println(); } } zin.closeEntry(); } }
2、读取文件,压缩zip并下载;
//java自带的类库,实现zip压缩打包文件下载, 不支持中文文件名 import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Date; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class test { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-Disposition", "attachment; filename=" + this.getZipFilename()); System.out.println("in BatchDownload................"); ZipOutputStream zos = new ZipOutputStream(response.getOutputStream()); File[] files = new File[2]; files[0] = new File("D:/OptiCM-POC/KDKK3Project/KDKK/images/1.jpg"); files[1] = new File("D:/OptiCM-POC/KDKK3Project/KDKK/images/2.jpg"); zipFile(files, "", zos); zos.flush(); zos.close(); } private void zipFile(File[] subs, String baseName, ZipOutputStream zos) throws IOException { for (int i = 0; i < subs.length; i++) { File f = subs[i]; zos.putNextEntry(new ZipEntry(baseName + f.getName())); FileInputStream fis = new FileInputStream(f); byte[] buffer = new byte[1024]; int r = 0; while ((r = fis.read(buffer)) != -1) { zos.write(buffer, 0, r); } fis.close(); } } private String getZipFilename() { Date date = new Date(); String s = date.getTime() + ".zip"; return s; } }
3、java操作文件(压缩文件,导出zip,删除zip,写入文件,读取文件)
//压缩文件(防止中文乱码----使用apache的ZipOutputStream包) private static void writeZip(String[] strs,String zipname,String temppath) throws IOException { String[] files = strs; OutputStream os = new BufferedOutputStream(new FileOutputStream(temppath+"//"+zipname)); ZipOutputStream zos = new ZipOutputStream(os); byte[] buf = new byte[8192]; int len; for (int i=0;i 0 ) { zos.write( buf, 0, len ); } zos.closeEntry(); bis.close(); } zos.setEncoding("GBK"); for(int i=0;i 0 ) //切忌这后面不能加 分号 ”;“ { os.write(buf, 0, len);//向客户端输出,实际是把数据存放在response中,然后web服务器再去response中读取 } in.close(); os.close(); deleteZip(temppath);//导出后删除zip /** * 追加文件:使用FileWriter---写入 */ public static void appendMethodB(String fileName, StringBuffer content) { try { //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件 FileWriter writer = new FileWriter(fileName, true); /*OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(fileName, true),"UTF-8"); osw.write(content); osw.close();*/ writer.write(content.toString()); writer.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 功能:Java读取txt文件的内容 * 步骤:1:先获得文件句柄 * 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取 * 3:读取到输入流后,需要读取生成字节流 * 4:一行一行的输出。readline()。 * 备注:需要考虑的是异常情况 * @param filePath */ public static ArrayList readTxtFile(String filePath){ ArrayList readList = new ArrayList(); try { String encoding="GBK"; File file=new File(filePath); if(file.isFile() && file.exists()){ //判断文件是否存在 InputStreamReader read = new InputStreamReader( new FileInputStream(file),encoding);//考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while((lineTxt = bufferedReader.readLine()) != null){ readList.add(lineTxt); } read.close(); }else{ System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); } return readList; }