FileOutputStream throws FileNotFoundException when UnZipping

前端 未结 7 1772
温柔的废话
温柔的废话 2020-12-11 04:19

I\'m using a class that I found through google to unzip a .zip file.. The .zip contains files and folder. The problem is that FileOutputStream throws

7条回答
  •  执念已碎
    2020-12-11 04:51

    I used to write from app widget to both internal and external Android memory with following code:

            URL adr = new URL(cleanUrl);
    
    
            HttpURLConnection urlConnection = (HttpURLConnection) adr.openConnection();
    
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);
            urlConnection.setReadTimeout(5000);
    
            urlConnection.connect();
    
            File file = new File(path, name);
    
            FileOutputStream fileOutput = new FileOutputStream(file);
    
            InputStream inputStream = urlConnection.getInputStream();
    
            byte[] buffer = new byte[1024];
            int bufferLength = 0; 
            while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                fileOutput.write(buffer, 0, bufferLength);
            }
            fileOutput.flush();
    
            fileOutput.close();
    

    Where path was both:

            path = mContext.getFilesDir();
    

    or

            path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    

    and

            path.mkdirs(); 
    

    In both cases I've got a FileNotFoundException and a created file with zero length.

    I've managed to write to both types of Android memory with a following function:

       protected InputStream get(String url) throws ClientProtocolException, IOException {
    
        final HttpClient client = new DefaultHttpClient();
    
        HttpGet getRequest = new HttpGet(url);
    
        HttpResponse response = client.execute(getRequest);
        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            return null;
        }
    
    
        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            try {
                return entity.getContent();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        return null;
    }
    

    And following usage:

              File file = new File(path, name);
    
            FileOutputStream fileOutput = new FileOutputStream(file);
            InputStream inputStream = get(cleanUrl);            
    
            byte[] buffer = new byte[1024];
            int bufferLength = 0;
            while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
    
                fileOutput.write(buffer, 0, bufferLength);
            }
            fileOutput.flush();
    
            fileOutput.close();
    

提交回复
热议问题