예제 #1
0
  public static int unZip(String unZipfileName, String mDestPath) {
    if (!mDestPath.endsWith("/")) {
      mDestPath = mDestPath + "/";
    }
    FileOutputStream fileOut = null;
    ZipInputStream zipIn = null;
    ZipEntry zipEntry = null;
    File file = null;
    int readedBytes = 0;
    byte buf[] = new byte[4096];
    try {
      zipIn = new ZipInputStream(new BufferedInputStream(new FileInputStream(unZipfileName)));
      while ((zipEntry = zipIn.getNextEntry()) != null) {
        file = new File(mDestPath + zipEntry.getName());
        if (zipEntry.isDirectory()) {
          file.mkdirs();
        } else {

          File parent = file.getParentFile();
          if (!parent.exists()) {
            parent.mkdirs();
          }
          fileOut = new FileOutputStream(file);
          while ((readedBytes = zipIn.read(buf)) > 0) {
            fileOut.write(buf, 0, readedBytes);
          }
          fileOut.close();
        }
        zipIn.closeEntry();
      }
      return 1;
    } catch (IOException ioe) {
      ioe.printStackTrace();
      return 0;
    } catch (Exception e) {
      e.printStackTrace();
      Logs.e(e, "");
      return 0;
    } finally {
      try {
        if (fileOut != null) fileOut.close();
        if (zipIn != null) zipIn.close();
      } catch (Exception e) {
        e.printStackTrace();
        Logs.e(e, "");
      }
    }
  }
예제 #2
0
  public static void unzipGzips(String inFileName) throws Exception {
    GZIPInputStream in = null;
    FileOutputStream out = null;
    try {

      if (!getExtension(inFileName).equalsIgnoreCase("gz")) {
        System.err.println("File name must have extension of \".gz\"");
        System.exit(1);
      }

      System.out.println("Opening the compressed file.");

      try {
        in = new GZIPInputStream(new FileInputStream(inFileName));
      } catch (FileNotFoundException e) {
        System.err.println("File not found. " + inFileName);
        System.exit(1);
      }

      System.out.println("Open the output file.");
      String outFileName = getFileName(inFileName);

      try {
        out = new FileOutputStream(outFileName);
      } catch (FileNotFoundException e) {
        System.err.println("Could not write to file. " + outFileName);
        System.exit(1);
      }

      System.out.println("Transfering bytes from compressed file to the output file.");
      byte[] buf = new byte[1024];
      int len;
      while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
      }

      System.out.println("Closing the file and stream");

    } catch (IOException e) {
      e.printStackTrace();
      System.exit(1);
    } finally {
      try {
        if (in != null) in.close();
        if (out != null) out.close();
      } catch (IOException e) {
        e.printStackTrace();
        Logs.e(e, "");
      }
    }
  }
예제 #3
0
  public static void zipFile(String fileFrom, String fileTo) {
    FileInputStream in = null;
    FileOutputStream out = null;
    ZipOutputStream zipOut = null;
    try {
      File file = new File(fileFrom);
      file.getName();
      in = new FileInputStream(fileFrom);
      out = new FileOutputStream(fileTo);
      zipOut = new ZipOutputStream(out);
      ZipEntry entry = new ZipEntry(file.getName());
      zipOut.putNextEntry(entry);
      int nNumber;
      byte[] buffer = new byte[512];
      while ((nNumber = in.read(buffer)) != -1) {
        zipOut.write(buffer, 0, nNumber);
      }
      zipOut.close();

      out.close();
      in.close();
      System.out.print("success" + file.getName());
    } catch (IOException e) {

      System.out.println(e);
    } finally {
      try {
        if (in != null) in.close();
        if (out != null) out.close();
        if (zipOut != null) zipOut.close();
      } catch (Exception e) {
        e.printStackTrace();
        Logs.e(e, "");
      }
    }
  }