0

How to copy my zip file (I have this file in my res files) to internal storage (path: data/data/[myPackageName]/files) ?

I tried to do so, but it only worked once and not anymore.

FileInputStream in = null;
InputStreamReader reader = null;
try {
    char[] inputBuffer = new char[256];
    in = openFileInput("file.zip");
    reader = new InputStreamReader(in);
    reader.read(inputBuffer);
    String myFile = new String(inputBuffer);
} catch (Exception e) {;}
finally {
    try {
        if (reader != null)reader.close();
    } catch (IOException e) {; }
    try {
        if (in != null)in.close();
    } catch (IOException e) {;}
}
AlexG
  • 1
  • 1
  • 1
    I don't see any code where you try to write a file. And the existing code for reading a file is not usable for ZIP files as uses a Reader which it is designed to work with text files and will fail with a binary ZIP file. – Robert Jun 01 '23 at 20:25
  • @Robert, I got it, but I tried with text file also and btw talking about writing a file, I think it need to only read file 'cause this file already exists, or not? But I will try to write and than read what you said) – AlexG Jun 02 '23 at 09:05
  • The title of your question is "how to store zip file to android internal storage" - storing a file means writing it not reading it. If you want to read a file please edit your question and correct title and body. – Robert Jun 02 '23 at 09:09
  • I need a send existing file from my project to file folder inside internal storage in phone, is it reading or writing? – AlexG Jun 02 '23 at 09:11
  • That is copying, reading from one InputStream of a file or asset and writing to an OutputStream of a second file. See e.g. here: https://stackoverflow.com/q/4447477/150978 Just the destination folder is different (external instead of internal storage). – Robert Jun 02 '23 at 09:17
  • @Robert, thx that link really seems with my issue) – AlexG Jun 02 '23 at 09:26

1 Answers1

0

I solved my issue))

Firstly, create folder assets along this path: .../com.example.yourappname/src/main/assets and place yourFileName there


Secondly use these methods:

        String dirPath = this.getFilesDir().getAbsolutePath() + "/yourCustomFolder/";
        File dir = new File(dirPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        AssetManager assetManager = getAssets();
        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(filename);
            File outFile = new File(dirPath, filename);
            out = new FileOutputStream(outFile);
            copyFile(in, out);
            Toast.makeText(this, "Saved!", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this, "Failed!", Toast.LENGTH_SHORT).show();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1){
            out.write(buffer, 0, read);
        }
    }```




-------------------------------------------------------------------------------

After all, use this to call a general method: copyAssets("yourFileName");
AlexG
  • 1
  • 1