All Products
Search
Document Center

File storage

Last Updated: May 25, 2021

The file storage provided by mPaaS provides file encryption capability based on Android native File.

Note: Because the file encryption uses the encryption function provided by Mobile Security Guard, you must ensure that the encryption images in Mobile Security Guard have been correctly generated.

File types

  • ZFile: This type of files are stored in data/data/package_name/files.
  • ZExternalFile: This type of files are stored in sdcard/Android/data/package_name/files.
  • ZFileInputStream/ZFileOutputStream: File storage input/output stream, the files will not be encrypted if you use this stream.
  • ZSecurityFileInputStream/ZSecurityFileOutputStream: Secure file storage input/output stream, the files will be encrypted if you use this stream.

Examples

Convert files to text

 
  1. /**
  2. * Convert files to text
  3. * @param file: File
  4. * @return Text
  5. */
  6. public String file2String(File file) {
  7. InputStreamReader reader = null;
  8. StringWriter writer = new StringWriter();
  9. try {
  10. // Use decryption input stream ZSecurityFileInputStream
  11. // If you don’t use encryption/decryption function, then use ZFileInputStream
  12. reader = new InputStreamReader(new ZSecurityFileInputStream(file, this));
  13. //Write the input stream into the output stream
  14. char[] buffer = new char[DEFAULT_BUFFER_SIZE];
  15. int n = 0;
  16. while (-1 != (n = reader.read(buffer))) {
  17. writer.write(buffer, 0, n);
  18. }
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. return null;
  22. } finally {
  23. if (reader != null)
  24. try {
  25. reader.close();
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. //Return the coversion result
  31. if (writer != null) {
  32. return writer.toString();
  33. } else {
  34. return null;
  35. }
  36. }

Convert text to files

 
  1. /**
  2. * Convert text to files
  3. * @param res: Text
  4. * @param file: File
  5. * @return true means successful while false means failed
  6. */
  7. public boolean string2File(String res, File file) {
  8. boolean flag = true;
  9. BufferedReader bufferedReader = null;
  10. BufferedWriter bufferedWriter;
  11. try {
  12. bufferedReader = new BufferedReader(new StringReader(res));
  13. // Use decryption output stream ZSecurityFileOutputStream
  14. // If you don’t use encryption/decryption function, then use ZFileOutputStream
  15. bufferedWriter = new BufferedWriter(new OutputStreamWriter(new ZSecurityFileOutputStream(file, this)));
  16. //Character buffers
  17. char buf[] = new char[DEFAULT_BUFFER_SIZE];
  18. int len;
  19. while ((len = bufferedReader.read(buf)) != -1) {
  20. bufferedWriter.write(buf, 0, len);
  21. }
  22. bufferedWriter.flush();
  23. bufferedReader.close();
  24. bufferedWriter.close();
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. flag = false;
  28. return flag;
  29. } finally {
  30. if (bufferedReader != null) {
  31. try {
  32. bufferedReader.close();
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. }
  38. return flag;
  39. }

Insert files

 
  1. /**
  2. * Insert files
  3. *
  4. * @param file: File
  5. */
  6. private void insertFile(BaseFile file) {
  7. if (null == file) {
  8. return;
  9. }
  10. StringBuilder sb = new StringBuilder();
  11. String content = sb.append(file.getName())
  12. .append(' ')
  13. .append(SIMPLE_DATE_FORMAT.format(new Date(System.currentTimeMillis()))).toString();
  14. string2File(content, file);
  15. try {
  16. if (!file.exists()) {
  17. file.createNewFile();
  18. }
  19. mData.add(file);
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. }

Delete files

 
  1. /**
  2. * Delete files
  3. *
  4. * @param file: File
  5. */
  6. private void deleteFile(BaseFile file) {
  7. try {
  8. file.delete();
  9. mData.remove(file);
  10. } catch (Exception e) {
  11. e.printStackTrace();
  12. }
  13. }