Deprecated: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/html/libraries/src/Document/Renderer/Feed/AtomRenderer.php on line 89 Projects https://www.isaacray.com/index.php/projects 2024-05-12T12:45:03+00:00 IsaacRay Joomla! - Open Source Content Management SMS Backup Restore Parser 2016-09-02T03:54:59+00:00 2016-09-02T03:54:59+00:00 https://www.isaacray.com/index.php/projects/1-sms-backup-restore-parser Super User <p style="font-size: 12.16px; line-height: 15.808px;">I use this <a href="https://play.google.com/store/apps/details?id=com.riteshsahu.SMSBackupRestore&amp;hl=en">SMS Backup and Restore</a> application on my android phone to export all my text messages.  What I really want, is to just get all the pictures and save them to a better location and to delete all the messages.  Unfortunately, this application only creates one large XML file with the images Base64 encoded into the XML.</p> <p style="font-size: 12.16px; line-height: 15.808px;">I do this when I want to factory reset my phone but don't want to lose all the media files people have sent me.</p> <p style="font-size: 12.16px; line-height: 15.808px;">I created this script to pull those images out and place them in a folder.  It is rudimentary.  The file names and directories are hard coded... whatever.  It does what I need, take it if you want.</p> <p style="font-size: 12.16px; line-height: 15.808px;"> </p> <table class="MsoTableGrid" style="cursor: default; border-collapse: collapse; border: none;" border="1" cellspacing="0" cellpadding="0"> <tbody> <tr> <td style="color: #000000; font-size: 10px; margin: 8px; cursor: text; width: 578.625px; border: 1pt solid windowtext; padding: 0in 5.4pt;" valign="top" width="638"> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">package com.isaacray.sms;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;"> </p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import java.io.BufferedReader;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import java.io.BufferedWriter;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import java.io.File;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import java.io.FileOutputStream;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import java.io.FileReader;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import java.io.IOException;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import java.io.OutputStreamWriter;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import java.util.Base64;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import java.util.Set;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import java.util.TreeSet;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;"> </p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import javax.xml.parsers.DocumentBuilder;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import javax.xml.parsers.DocumentBuilderFactory;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;"> </p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import org.w3c.dom.Document;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import org.w3c.dom.Node;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import org.w3c.dom.NodeList;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;"> </p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">public class Main {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">public static void main(String[] argsv) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">p("Beginning program");</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">String filename = "C:\\sms-20150814082858.xml";</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">String tmpFileName = filename + ".tmp";</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">String outputDir = "C:\\output\\";</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">try {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">cleanUpFile(filename, tmpFileName);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">Document doc = dBuilder.parse(new File(tmpFileName));</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">NodeList parts = doc.getElementsByTagName("part");</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">// printMimeTypes(parts);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">new File(outputDir).mkdirs();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">for (int i = 0; i &lt; parts.getLength(); ++i) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">p("Processing " + (i + 1) + " of " + parts.getLength());</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">Node part = parts.item(i);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">String ct = part.getAttributes().getNamedItem("ct").toString();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">if (ct.contains("image/") || ct.contains("video/") || ct.contains("audio/")) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">byte[] bytes = Base64.getDecoder().decode(part.getAttributes().getNamedItem("data").getTextContent());</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">String name = part.getAttributes().getNamedItem("name").getTextContent();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">if (name.equals("null")) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">name = i + part.getAttributes().getNamedItem("cl").getTextContent();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">FileOutputStream fos = new FileOutputStream(outputDir + name);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">fos.write(bytes);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">fos.close();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">File file = new File(outputDir + name);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">Node parent = part.getParentNode().getParentNode();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">file.setLastModified(Long.valueOf(parent.getAttributes().getNamedItem("date").getTextContent()));</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">} catch (Exception e) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">p(e);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">} finally {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">try {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">String command = "cmd /C del \"" + tmpFileName + "\"";</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">Runtime.getRuntime().exec(command);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">} catch (Exception e) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">p(e);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;"> </p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">private static void cleanUpFile(String filename, String tmpFileName) throws IOException {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">BufferedReader br = new BufferedReader(new FileReader(filename));</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">FileOutputStream fos = new FileOutputStream(tmpFileName);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos, "UTF8"));</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">while (br.ready()) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">String line = br.readLine();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">bw.write(line.replaceAll("&amp;#[0-9][0-9][0-9][0-9][0-9];", ""));</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">bw.newLine();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">br.close();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">bw.close();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;"> </p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">private static void printMimeTypes(NodeList parts) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">Set&lt;String&gt; mimes = new TreeSet&lt;String&gt;();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">for (int i = 0; i &lt; parts.getLength(); ++i) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">Node part = parts.item(i);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">String ct = part.getAttributes().getNamedItem("ct").toString();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">mimes.add(ct);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">for (String mime : mimes) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">p(mime);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;"> </p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">private static void p(Object o) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">System.out.println(o);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> </td> </tr> </tbody> </table> <p style="font-size: 12.16px; line-height: 15.808px;"> </p> <p style="font-size: 12.16px; line-height: 15.808px;">I use this <a href="https://play.google.com/store/apps/details?id=com.riteshsahu.SMSBackupRestore&amp;hl=en">SMS Backup and Restore</a> application on my android phone to export all my text messages.  What I really want, is to just get all the pictures and save them to a better location and to delete all the messages.  Unfortunately, this application only creates one large XML file with the images Base64 encoded into the XML.</p> <p style="font-size: 12.16px; line-height: 15.808px;">I do this when I want to factory reset my phone but don't want to lose all the media files people have sent me.</p> <p style="font-size: 12.16px; line-height: 15.808px;">I created this script to pull those images out and place them in a folder.  It is rudimentary.  The file names and directories are hard coded... whatever.  It does what I need, take it if you want.</p> <p style="font-size: 12.16px; line-height: 15.808px;"> </p> <table class="MsoTableGrid" style="cursor: default; border-collapse: collapse; border: none;" border="1" cellspacing="0" cellpadding="0"> <tbody> <tr> <td style="color: #000000; font-size: 10px; margin: 8px; cursor: text; width: 578.625px; border: 1pt solid windowtext; padding: 0in 5.4pt;" valign="top" width="638"> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">package com.isaacray.sms;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;"> </p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import java.io.BufferedReader;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import java.io.BufferedWriter;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import java.io.File;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import java.io.FileOutputStream;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import java.io.FileReader;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import java.io.IOException;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import java.io.OutputStreamWriter;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import java.util.Base64;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import java.util.Set;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import java.util.TreeSet;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;"> </p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import javax.xml.parsers.DocumentBuilder;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import javax.xml.parsers.DocumentBuilderFactory;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;"> </p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import org.w3c.dom.Document;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import org.w3c.dom.Node;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">import org.w3c.dom.NodeList;</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;"> </p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">public class Main {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">public static void main(String[] argsv) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">p("Beginning program");</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">String filename = "C:\\sms-20150814082858.xml";</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">String tmpFileName = filename + ".tmp";</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">String outputDir = "C:\\output\\";</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">try {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">cleanUpFile(filename, tmpFileName);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">Document doc = dBuilder.parse(new File(tmpFileName));</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">NodeList parts = doc.getElementsByTagName("part");</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">// printMimeTypes(parts);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">new File(outputDir).mkdirs();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">for (int i = 0; i &lt; parts.getLength(); ++i) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">p("Processing " + (i + 1) + " of " + parts.getLength());</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">Node part = parts.item(i);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">String ct = part.getAttributes().getNamedItem("ct").toString();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">if (ct.contains("image/") || ct.contains("video/") || ct.contains("audio/")) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">byte[] bytes = Base64.getDecoder().decode(part.getAttributes().getNamedItem("data").getTextContent());</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">String name = part.getAttributes().getNamedItem("name").getTextContent();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">if (name.equals("null")) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">name = i + part.getAttributes().getNamedItem("cl").getTextContent();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">FileOutputStream fos = new FileOutputStream(outputDir + name);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">fos.write(bytes);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">fos.close();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">File file = new File(outputDir + name);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">Node parent = part.getParentNode().getParentNode();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">file.setLastModified(Long.valueOf(parent.getAttributes().getNamedItem("date").getTextContent()));</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">} catch (Exception e) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">p(e);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">} finally {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">try {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">String command = "cmd /C del \"" + tmpFileName + "\"";</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">Runtime.getRuntime().exec(command);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">} catch (Exception e) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">p(e);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;"> </p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">private static void cleanUpFile(String filename, String tmpFileName) throws IOException {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">BufferedReader br = new BufferedReader(new FileReader(filename));</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">FileOutputStream fos = new FileOutputStream(tmpFileName);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos, "UTF8"));</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">while (br.ready()) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">String line = br.readLine();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">bw.write(line.replaceAll("&amp;#[0-9][0-9][0-9][0-9][0-9];", ""));</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">bw.newLine();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">br.close();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">bw.close();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;"> </p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">private static void printMimeTypes(NodeList parts) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">Set&lt;String&gt; mimes = new TreeSet&lt;String&gt;();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">for (int i = 0; i &lt; parts.getLength(); ++i) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">Node part = parts.item(i);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">String ct = part.getAttributes().getNamedItem("ct").toString();</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">mimes.add(ct);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">for (String mime : mimes) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">p(mime);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;"> </p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">private static void p(Object o) {</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">System.out.println(o);</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;">}</p> </td> </tr> </tbody> </table> <p style="font-size: 12.16px; line-height: 15.808px;"> </p>