There is a url by which the * .gz archive comes. When I unzip it, I get an * .zip archive. I need to get this .gz archive without saving it on the device, then pull out the .zip, unzip it and save it to a folder on the device.

ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream is = null; try { is = url.openStream(); byte[] byteChunk = new byte[4096]; int n; while ((n = is.read(byteChunk)) > 0) { baos.write(byteChunk, 0, n); } GZIPOutputStream gzipOutputStream = new GZIPOutputStream(baos); ZipOutputStream zipOutputStream = new ZipOutputStream(gzipOutputStream); } catch (IOException e) { e.printStackTrace(); } 

It seems to be nonsense and I do not understand something about working with i / o streams. Please explain)

    2 answers 2

    It looks like you are using * OutputStream where you need to use * Inputstream.

    It should be like this:

      URL url = new URL("http://example.com/data.json.gz"); URLConnection urlConnection = url.openConnection(); GZIPInputStream gzipInputStream = new GZIPInputStream(urlConnection.getInputStream()); 

    And then screw up the ZipInputStream. There is also note that in the zip archive may contain several entities / files.

    • Not in GZIP format. - ManGust
    • Although I'm sure that the tar.gz ZipInputStream will come, but then the ZipEntry is empty ( - ManGust

    I decided to answer the question myself. This code helps to unzip tar.gz, get what is inside and scatter into folders. I hope this will help someone)

     InputStream in = new ByteArrayInputStream(downloadFile("http://urlFile")); final List<File> untaredFiles = new LinkedList<>(); TarArchiveInputStream tarInput = new TarArchiveInputStream(new GzipCompressorInputStream(in)); TarArchiveEntry entry; final File path = new File(strings[0] + "/Folder"); if (!path.exists()){ path.mkdirs(); } while ((entry = (TarArchiveEntry)tarInput.getNextEntry()) != null) { final File outputFile = new File(path, entry.getName()); if(entry.isFile()) { if(!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } byte[] bytes = new byte[(int)entry.getSize()]; tarInput.read(bytes); FileOutputStream stream = new FileOutputStream(outputFile); stream.write(bytes); stream.close(); } if(entry.isDirectory()) { continue; } untaredFiles.add(outputFile); } 

    DownloadFile Method:

      public static byte[] downloadFile(String urlString) throws Exception { HttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(urlString); HttpResponse response = client.execute(httpGet); return EntityUtils.toByteArray(response.getEntity()); } 

    The code is damp, but it works.