Example #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();
  }
Example #2
0
  /**
   * 将目录压缩到ZIP输出流
   *
   * @param dirPath 目录路径
   * @param fileDir 文件信息
   * @param zouts 输出流
   */
  public static void zipDirectoryToZipFile(String dirPath, File fileDir, ZipOutputStream zouts) {
    if (fileDir.isDirectory()) {
      File[] files = fileDir.listFiles();
      // 空的文件夹
      if (files.length == 0) {
        // 目录信息
        ZipEntry entry = new ZipEntry(getEntryName(dirPath, fileDir));
        try {
          zouts.putNextEntry(entry);
          zouts.closeEntry();
        } catch (Exception e) {
          e.printStackTrace();
        }
        return;
      }

      for (File file : files) {
        if (file.isFile()) {
          // 如果是文件,则调用文件压缩方法
          FileUtils.zipFilesToZipFile(dirPath, file, zouts);
        } else {
          // 如果是目录,则递归调用
          FileUtils.zipDirectoryToZipFile(dirPath, file, zouts);
        }
      }
    }
  }
Example #3
0
 /**
  * 将文件压缩到ZIP输出流
  *
  * @param dirPath 目录路径
  * @param file 文件
  * @param zouts 输出流
  */
 public static void zipFilesToZipFile(String dirPath, File file, ZipOutputStream zouts) {
   FileInputStream fin = null;
   ZipEntry entry = null;
   // 创建复制缓冲区
   byte[] buf = new byte[4096];
   int readByte = 0;
   if (file.isFile()) {
     try {
       // 创建一个文件输入流
       fin = new FileInputStream(file);
       // 创建一个ZipEntry
       entry = new ZipEntry(getEntryName(dirPath, file));
       // 存储信息到压缩文件
       zouts.putNextEntry(entry);
       // 复制字节到压缩文件
       while ((readByte = fin.read(buf)) != -1) {
         zouts.write(buf, 0, readByte);
       }
       zouts.closeEntry();
       fin.close();
       System.out.println("添加文件 " + file.getAbsolutePath() + " 到zip文件中!");
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
 }
Example #4
0
  /**
   * Adds a single file into the ZipOutputStream with specified entry name.
   *
   * @throws IOException
   */
  private void addFileToZip(ZipOutputStream zipOut, UploadedFile file, String name)
      throws IOException {
    if (log.isTraceEnabled()) {
      log.trace(">> addFileToZip(): " + file);
    }

    ZipEntry entry = new ZipEntry(name);
    zipOut.putNextEntry(entry);

    InputStream in = null;
    try {
      in = file.getInputstream();
      FileUtils.copyStream(file.getInputstream(), zipOut);
      zipOut.closeEntry();
    } finally {
      FileUtils.close(in);
    }

    //        try (InputStream in = file.getInputstream()) {
    //            FileUtils.copyStream(file.getInputstream(), zipOut);
    //            zipOut.closeEntry();
    //        }

    if (log.isTraceEnabled()) {
      log.trace("<< addFileToZip()");
    }
  }
Example #5
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);
   }
 }
  /**
   * Adds a single file into the ZipOutputStream with specified entry name.
   *
   * @throws IOException
   */
  private void addFileToZip(ZipOutputStream zipOut, ReshakaUploadedFile file, String name)
      throws IOException {
    if (log.isTraceEnabled()) {
      log.trace(">> addFileToZip(): " + file);
    }

    ZipEntry entry = new ZipEntry(name);
    zipOut.putNextEntry(entry);
    try (InputStream in = file.getInputstream()) {
      FileUtils.copyStream(file.getInputstream(), zipOut);
      zipOut.closeEntry();
    }

    if (log.isTraceEnabled()) {
      log.trace("<< addFileToZip()");
    }
  }
Example #8
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();
   }
 }
Example #9
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();
 }
Example #10
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;
  }