public static void createZipFile(){ String [] fileNames = {"E:\\upload\\glentry\\test1.txt","E:\\upload\\glentry\\test2.txt", "E:\\upload\\glentry\\test3.txt","E:\\upload\\glentry\\test4.txt"}; String compressFileName = "E:\\upload\\glentry\\text.zip"; byte b[] = new byte[1024]; try { ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(compressFileName)); for(int i = 0; i < fileNames.length; i++){ InputStream in = new FileInputStream(fileNames[i]); File file=new File(fileNames[i]); String filename = file.getName();//取得文件名 ZipEntry e = new ZipEntry(filename); //压缩后不带路径 zout.putNextEntry(e); int len=0; while((len=in.read(b)) != -1){ zout.write(b,0,len); } zout.closeEntry(); } zout.finish(); zout.close(); } catch (FileNotFoundException e) { e.printStackTrace(); }catch(IOException io){ io.printStackTrace(); } }
使用JAVA生成zip文件的方法:
最新推荐文章于 2024-06-11 10:15:07 发布