在不使用任何文件路径的情况下解码和解压缩文件

时间:2018-05-28 10:04:23

标签: java

我正在尝试解码一个文件并对其进行解压缩.Input是一个用base 64编码编码的GZIP文件。但由于我不能在我的代码中使用任何文件路径,因此需要在代码中遇到问题。

1 个答案:

答案 0 :(得分:0)

// ** import statements

 String GZIP_NEW_FILE = "C:\\Users\\xyz\\Desktop\\Tar\\Final.tar.gz";

以下代码有助于在不使用文件路径的情况下解码和解压缩文件。

Fist我正在编码tar文件:

private static String encodeFileToBase64Binary(String fileName) throws IOException {
            File file = new File(fileName);
            byte[] encoded = Base64.encodeBase64(FileUtils.readFileToByteArray(file));
            return new String(encoded);
        }

// 编码tar文件

private static byte[] decodeFileToBase64Binary(String base64String) throws IOException {

            return Base64.decodeBase64(base64String);
        }

// 解码tar文件

public static String deCompressGZipFile(byte [] decodedBytes) 
        throws IOException{

      InputStream is = new ByteArrayInputStream(decodedBytes);
      GZIPInputStream gZIPInputStream = new GZIPInputStream(is);
      InputStreamReader reader = new InputStreamReader(gZIPInputStream);
      BufferedReader in = new BufferedReader(reader);
      StringBuffer sb=new StringBuffer();
      String readed;
      while ((readed = in.readLine()) != null) {
          sb.append(readed);
      }
      gZIPInputStream.close();
      return sb.toString().substring(sb.toString().indexOf("{"));
       }

// 解压缩已解码的gzip文件

FMessaging.messaging().delegate = self
Messaging.messaging().shouldEstablishDirectChannel = true

FirebaseApp.configure()

application.registerForRemoteNotifications()