Question

I want to change my Android application behave from reading a text file inside the APK to read it directly from a RAR or ZIP file with password protection (I think that it may be the same protection as when it is coded inside my APK). That is the same project in which I asked this question: How to convert an UTF String to ANSI and Create an ANSI text file in SSD with JAVA-ANDROID

But I do not know how to do it. When I want to change this file contents I always have to recompile my APK to send again to each specific client. If I put this file in a password protected compressed archive, I may only deploy this file and tell the client to put in a specific path on the cellphone SSD.

My application should extract the file into a String (it is a text file) or extract it into a file in the SSD (it is less secure and I will need to delete the file after read its contents). How can I do it? Will I need some third party library? Can you show me the code? I am just begging in Java.

Was it helpful?

Solution

You can try zip4j, and do this:

public String extractInputStream() {

    ZipInputStream is = null;
    String extracted = null;

    try {
        // get the zip
        ZipFile zipFile = new ZipFile("/path/to/my/file.zip");

        // set the password
        if (zipFile.isEncrypted()) {
            zipFile.setPassword("password");
        }
        //get the file inside the zip
        FileHeader fileHeader = zipFile.getFileHeader("yourfile.txt");

            if (fileHeader != null) {
                        is = zipFile.getInputStream(fileHeader);
                        //edited
                        extracted = new Scanner(is,"UTF-8").useDelimiter("\\A").next();
                        //edited
                        is.close();

        }
    } catch (ZipException e) {
        e.printStackTrace(); //make something better here
    } catch (FileNotFoundException e) {
        e.printStackTrace();//make something better here
    } catch (IOException e) {
        e.printStackTrace();//make something better here
    } catch (Exception e) {
        e.printStackTrace();//make something better here
    } 
    return extracted;
}

I did not test this code, but you can do it, and improve it ;-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top