Ejemplo n.º 1
0
  public void writeblob() throws IOException {

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream("c://test2.zip"));

    File f = new File("c://abc.doc");
    FileInputStream in = new FileInputStream(f);
    byte a[] = null; // **将测试文件读入此字节数组
    int flength = (int) f.length(); // **读入文件的字节长度
    System.out.println("file length::" + flength);
    a = new byte[flength];

    int i = 0;
    int itotal = 0;
    // 将文件读入字节数组
    for (; itotal < flength; itotal = i + itotal) {

      // in.read

      i = in.read(a, itotal, flength - itotal);
    }
    out.putNextEntry(new org.apache.tools.zip.ZipEntry("test2"));
    out.write(a);
    in.close();
    out.close();
  }
Ejemplo n.º 2
0
 /**
  * 压缩文件或目录
  *
  * @param srcDirName 压缩的根目录
  * @param fileName 根目录下的待压缩的文件名或文件夹名,其中*或""表示跟目录下的全部文件
  * @param descFileName 目标zip文件
  */
 public static void zipFiles(String srcDirName, String fileName, String descFileName) {
   // 判断目录是否存在
   if (srcDirName == null) {
     log.debug("文件压缩失败,目录 " + srcDirName + " 不存在!");
     return;
   }
   File fileDir = new File(srcDirName);
   if (!fileDir.exists() || !fileDir.isDirectory()) {
     log.debug("文件压缩失败,目录 " + srcDirName + " 不存在!");
     return;
   }
   String dirPath = fileDir.getAbsolutePath();
   File descFile = new File(descFileName);
   try {
     ZipOutputStream zouts = new ZipOutputStream(new FileOutputStream(descFile));
     if ("*".equals(fileName) || "".equals(fileName)) {
       FileUtils.zipDirectoryToZipFile(dirPath, fileDir, zouts);
     } else {
       File file = new File(fileDir, fileName);
       if (file.isFile()) {
         FileUtils.zipFilesToZipFile(dirPath, file, zouts);
       } else {
         FileUtils.zipDirectoryToZipFile(dirPath, file, zouts);
       }
     }
     zouts.close();
     log.debug(descFileName + " 文件压缩成功!");
   } catch (Exception e) {
     log.debug("文件压缩失败:" + e.getMessage());
     e.printStackTrace();
   }
 }
Ejemplo n.º 3
0
  private byte[] zip(ZipOutputStream out, File f, String base) throws Exception {

    //		System.out.println("isDirectory===="+f.isDirectory());
    //		if (f.isDirectory()) {
    //			File[] fl = f.listFiles();
    //			out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
    //			base = base.length() == 0 ? "" : base + "/";
    //			for (int i = 0; i < fl.length; i++) {
    //				zip(out, fl[i], base + fl[i].getName());
    //			}
    //
    //
    //		} else {
    out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
    FileInputStream in = new FileInputStream(f);

    // ZipInputStream  zis  = new ZipInputStream ();

    /**
     * int b; while ((b = in.read()) != -1) {
     *
     * <p>out.write(b); }
     *
     * <p>in.close();
     */
    byte a[] = null; // **将测试文件读入此字节数组
    int flength = (int) f.length(); // **读入文件的字节长度
    System.out.println("file length::" + flength);
    a = new byte[flength];

    int i = 0;
    int itotal = 0;
    // 将文件读入字节数组
    for (; itotal < flength; itotal = i + itotal) {

      // in.read

      i = in.read(a, itotal, flength - itotal);
    }

    System.out.println("压缩前的数字==" + a.length);
    out.write(a);
    byte[] comby = ZipFile.compressBytes(a);
    System.out.println("压缩后的数字==" + comby.length);

    byte[] decomby = ZipFile.decompressBytes(comby);

    System.out.println("解压缩后的数字==" + decomby.length);
    in.close();
    out.close();

    return a;

    //		}
  }
 private void zipFiles(List<String> srcfile, ByteArrayOutputStream bos) {
   try {
     ZipOutputStream out = new ZipOutputStream(bos);
     out.setEncoding("GBK");
     for (int i = 0; i < srcfile.size(); i++) {
       out.putNextEntry(new ZipEntry(srcfile.get(i)));
       out.write(bos.toByteArray());
     }
     out.closeEntry();
     out.flush();
     out.finish();
     out.close();
   } catch (IOException e) {
     e.printStackTrace();
     logger.error("ZipUtil zipFiles exception:" + e);
   }
 }
Ejemplo n.º 5
0
 /**
  * 压缩文件
  *
  * @param srcfile File[] 需要压缩的文件列表
  * @param zipfile File 压缩后的文件
  */
 public static void ZipFiles(java.io.File[] srcfile, java.io.File zipfile) {
   byte[] buf = new byte[1024];
   try {
     ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
     for (int i = 0; i < srcfile.length; i++) {
       FileInputStream in = new FileInputStream(srcfile[i]);
       out.putNextEntry(new ZipEntry(srcfile[i].getName()));
       String str = srcfile[i].getName();
       int len;
       while ((len = in.read(buf)) > 0) {
         out.write(buf, 0, len);
       }
       out.closeEntry();
       in.close();
     }
     out.close();
     System.out.println("压缩完成.");
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 6
0
 /**
  * zip压缩功能. 压缩baseDir(文件夹目录)下所有文件,包括子目录
  *
  * @throws Exception
  */
 public static void zipFile(String baseDir, String fileName, String comment) throws Exception {
   List fileList = getSubFiles(new File(baseDir));
   ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(fileName));
   ZipEntry ze = null;
   byte[] buf = new byte[BUFFER];
   int readLen = 0;
   for (int i = 0; i < fileList.size(); i++) {
     File f = (File) fileList.get(i);
     ze = new ZipEntry(getAbsFileName(baseDir, f));
     ze.setSize(f.length());
     ze.setTime(f.lastModified());
     zos.putNextEntry(ze);
     InputStream is = new BufferedInputStream(new FileInputStream(f));
     while ((readLen = is.read(buf, 0, BUFFER)) != -1) {
       zos.write(buf, 0, readLen);
     }
     is.close();
   }
   zos.setComment(comment);
   zos.close();
 }
Ejemplo n.º 7
0
  /** 压缩文件zip-Apache中ant所带包 @功能信息 : @参数信息 : @返回值信息 : @异常信息 : */
  public static String getZip(List list, String path, String fileName) throws Exception {
    byte[] buffer = new byte[1024];

    String strZipName = fileName + ".zip";
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(path + strZipName));
    for (int j = 0; j < list.size(); j++) {
      String name = list.get(j).toString();
      FileInputStream fis = new FileInputStream(path + name);
      out.putNextEntry(new ZipEntry(name));
      int len;
      while ((len = fis.read(buffer)) > 0) {
        out.write(buffer, 0, len);
      }
      out.closeEntry();
      fis.close();
      new File(path + name).delete();
    }
    out.close();
    String zipFilePath = path + strZipName;
    //		System.out.println(zipFilePath+"----");
    //		System.out.println("生成Demo.zip成功");
    return zipFilePath;
  }
  /**
   * 生成房源包
   *
   * @param house
   * @return
   * @throws IOException
   * @throws SQLException
   * @throws JRException
   */
  @SuppressWarnings({"rawtypes", "unchecked"})
  public Map<String, String> createHousePackage(Room room)
      throws IOException, JRException, SQLException {
    Map<String, String> housePackage = new HashMap<String, String>();
    // 临时存储文件路径
    System.out.println("读取房源包路径");
    URL s = HouseService.class.getClassLoader().getResource("");
    String tempDir =
        s.getPath().substring(0, s.getPath().lastIndexOf("WEB-INF")) + "//housePackage/";
    // String tempDir = ResourceManager.getString("zip.file.path");
    // 根据房间编号获得房间图片信息,小区图片信息,公共区域信息等
    Room r = houseAndRoomMapper.selectByRoomId(room.getRoomId());
    House h = houseAndRoomMapper.selectHouseByCode(r.getSysHouseId());

    // 房源包名称
    String fileName = h.getHouseCode() + r.getRoomName() + ".zip";
    String file = tempDir + "//" + fileName;
    housePackage.put("fileName", fileName);
    housePackage.put("downFilePath", file);
    // 文件加锁
    FileOutputStream fos = new FileOutputStream(file);
    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
    zos.setEncoding("GBK");
    // 图片服务器路径
    String imageUrl = ResourceManager.getString("image.url");
    // 室内图片
    List<RoomPictures> rps = houseAndRoomMapper.selectRoomPicturesByRoomId(r.getSysRoomId());
    int i = 0;
    long start = System.currentTimeMillis();
    for (RoomPictures rp : rps) {
      String path = rp.getImagepath();
      if (path != null && !path.equals("")) {
        int _a = path.lastIndexOf("/");
        path = path.substring(0, _a) + "//v480x360_" + path.substring(_a + 1);
        URL url = new URL(imageUrl + "//" + path);
        InputStream is = url.openStream();
        String _fileName = "室内图片/" + (i++) + ".jpg";
        CompressionUtil.compressFile(zos, is, _fileName);
        is.close();
      }
    }

    // 公共区域图片
    List<RoomPictures> hps = houseAndRoomMapper.selectRoomPicturesByHouseId(r.getSysHouseId());
    i = 0;
    for (RoomPictures rp : hps) {
      String path = rp.getImagepath();
      if (path != null && !path.equals("")) {
        int _a = path.lastIndexOf("/");
        path = path.substring(0, _a) + "//v480x360_" + path.substring(_a + 1);
        URL url = new URL(imageUrl + "//" + path);
        InputStream is = url.openStream();
        String _fileName = "公共区域图片/" + (i++) + ".jpg";
        CompressionUtil.compressFile(zos, is, _fileName);
        is.close();
      }
    }
    long end = System.currentTimeMillis();
    System.out.println("图片打包时间:" + (end - start) / 1000);

    // 物品配置家具
    Map map = new HashMap();
    map.put("sysRoomId", r.getSysRoomId());
    map.put("itemType", "jj4");
    map.put("sysHouseId", r.getSysHouseId());
    List<String> _c1 = houseAndRoomMapper.selectItemByRoom(map);
    List<String> _c2 = houseAndRoomMapper.selectItemByPub(map);
    // 家电
    map.put("itemType", "jd");
    List<String> _c3 = houseAndRoomMapper.selectItemByRoom(map);
    List<String> _c4 = houseAndRoomMapper.selectItemByPub(map);
    // 家居
    map.put("itemType", "jj1");
    List<String> _c5 = houseAndRoomMapper.selectItemByRoom(map);
    List<String> _c6 = houseAndRoomMapper.selectItemByPub(map);
    System.out.println("读取jasper文件");
    // 其它房屋租住状态
    StringBuilder sb = new StringBuilder();
    List<Map> rooms = houseAndRoomMapper.selectOtherRoom(map);
    int count = 0;
    for (int j = 0; j < rooms.size(); j++) {
      Map rm = rooms.get(j);
      String rentstatus = rm.get("rentstatus") != null ? rm.get("rentstatus").toString() : "";
      String position = rm.get("roomposition") != null ? rm.get("roomposition").toString() : "";
      if (rentstatus.equals("ycz")) {
        String sex = rm.get("sex") != null ? rm.get("sex").toString() : "";
        String professional =
            rm.get("professional") != null ? rm.get("professional").toString() : "";
        String code = rm.get("code") != null ? rm.get("code").toString() : "";
        ;
        sb.append(!position.equals("") ? (position + "卧" + code + "住着一位") : "");
        sb.append(!sex.equals("") ? sex + "性" : "");
        sb.append(!professional.equals("") ? professional : "");
        sb.append(";");
      } else {
        sb.append(!position.equals("") ? (position + "卧待租 ;") : "卧室待租; ");
        count++;
      }
    }
    // 房间待租数量
    if (count == rooms.size()) {
      sb = new StringBuilder();
    }
    // 房源word文档
    URL u = this.getClass().getClassLoader().getResource("cn/ziroom/house/service/package.jasper");
    System.out.println("文件路径:" + u.getPath());
    String reportFile = u.getFile();
    Map parameter = new HashMap();
    parameter.put("roomId", room.getRoomId());
    parameter.put("c1", StringUtils.split(_c1, ","));
    parameter.put("c2", StringUtils.split(_c2, ","));
    parameter.put("c3", StringUtils.split(_c3, ","));
    parameter.put("c4", StringUtils.split(_c4, ","));
    parameter.put("c5", StringUtils.split(_c5, ","));
    parameter.put("c6", StringUtils.split(_c6, ","));
    parameter.put("c7", sb.toString());
    start = System.currentTimeMillis();
    System.out.println("填充报表");
    // docx
    String filePath = JasperreportsUtils.docx(reportFile, parameter, dataSource.getConnection());
    File _file = new File(filePath);
    FileInputStream fis = new FileInputStream(_file);
    CompressionUtil.compressFile(zos, fis, h.getHouseCode() + r.getRoomName() + ".docx");
    fis.close();
    _file.delete();
    // doc
    String docFilePath = JasperreportsUtils.doc(reportFile, parameter, dataSource.getConnection());
    File _docfile = new File(docFilePath);
    FileInputStream docfis = new FileInputStream(_docfile);
    CompressionUtil.compressFile(zos, docfis, h.getHouseCode() + r.getRoomName() + ".doc");
    fis.close();
    _docfile.delete();

    byte[] html = JasperreportsUtils.html(reportFile, parameter, dataSource.getConnection());
    InputStream is = new ByteArrayInputStream(html);
    CompressionUtil.compressFile(zos, is, h.getHouseCode() + r.getRoomName() + ".html");

    end = System.currentTimeMillis();
    System.out.println("报表生成时间:" + (end - start) / 1000);
    is.close();
    zos.close();
    fos.close();
    return housePackage;
  }