FileOutputStream throws FileNotFoundException when UnZipping

前端 未结 7 1770
温柔的废话
温柔的废话 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:50

    Your first version relies on dirChecker() which you haven't posted, but either it doesn't work correctly, or your ZIP file doesn't contain directory entries at all, which is prefectly legal, so you shouldn't rely on them being present anyway.

    Your second version is better but you don't need all this:

     File f = new File(Path+FileName);
              if(!f.exists())
              {
                  f.mkdirs();
                  if(!f.createNewFile())
                  {
                      f.delete();
                      f.createNewFile();
                  }
              }
    

    You just need this:

    File f = new File(Path, FileName);
    f.getParentFile().mkdirs();
    

    The rest will happen anyway.

    0 讨论(0)
  • 2020-12-11 04:50

    The problem here is caused due to zip files or directories nested within the given zip. In case the given ZipEntry is a directory, you need to create the directory and skip writing with the FileOutputStream.

    use zipEnrty.isDirectory() to check whether the given zipEntry is a directory and create the directory using zipEntry.mkDirs() and proceed to the next zipEntry without writing it using fileOutputStream.

    Here is the full code for unzipping a zip file :

    import java.io.*;
    import java.util.zip.*;
    
    public class Unzip
    {
        public static void main (String args[])
        {
            try
            {
                final int BUFFER = 1024;
                int count;
                byte data[] = new byte[BUFFER];
                String path="res/Example.zip";
    
                BufferedOutputStream bufferedOutputStream = null;
                FileInputStream fileInputStream = new FileInputStream(path);
                ZipInputStream zipInputStream = new ZipInputStream(new  BufferedInputStream(fileInputStream));
                ZipEntry zipEntry;
    
                while((zipEntry = zipInputStream.getNextEntry()) != null)
                {
                    System.out.println("Extracting: " +zipEntry);
                    File file=new File(zipEntry.getName());
                    if(zipEntry.isDirectory())
                    {
                        file.mkdirs();
                        continue;
                    }
                    FileOutputStream fileOutputStream = new     FileOutputStream(file,false);
                    bufferedOutputStream = new BufferedOutputStream(fileOutputStream, BUFFER);
    
                    while ((count = zipInputStream.read(data, 0, BUFFER))!= -1)
                    bufferedOutputStream.write(data, 0, count);
    
                    bufferedOutputStream.flush();
                    bufferedOutputStream.close();
                }
                zipInputStream.close();
            } 
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    
    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 2020-12-11 04:56

    From the javadocs for the FileOutputStream constructor

    Throws:
    FileNotFoundException - if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason

    Often, a FileNotFoundException occurs if you do not have permissions to create files or that part of the file system is read-only (though I'm not sure to what extent this applies in Android)

    0 讨论(0)
  • 2020-12-11 05:00

    I get the FileNotFoundException again with (is a directory) message error.

    File f = new File(Path+FileName);
    f.mkdirs();
    

    Try using

    File directory = new File(Path);
    directory.mkdirs();
    

    and then

    File file = new File(directory, FileName);
    

    instead of your code.

    0 讨论(0)
  • 2020-12-11 05:07

    Remove the "android:maxSdkVersion="18" " from Manifest

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
       android:maxSdkVersion="18"/>
    

    You declared in Manifest file.

     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        />
    
    0 讨论(0)
提交回复
热议问题