How to Compress XML Java
When you're a software engineer, you're constantly looking for ways to optimize your application. By compressing any extensible markup language, or XML, that's generated by your application, you decrease both the file size of the application and the overall size of its file cache.
Instructions
-
-
1
Open the text editing application which you use to program in Java and are using to manage your Java application's development.
-
2
Use the "ZipOutputStream" command to compress the XML that is generated. Add the following lines of code to your application to enable file compression. Replace "example.zip" with the actual name you wish to use:
FileOutputStream dest = new
FileOutputStream("example.zip")
ZipOutputStream out = new
ZipOutputStream(new BufferedOutputStream(dest));
-
-
3
Create an entry for each compressed XML zip file by adding the following line of code:
ZipEntry entry = new ZipEntry(files[i]))
-
4
Write the actual XML data to the new zip file that is being created by adding the following lines of code:
int count;
while((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
-
5
Complete the compression by adding input and output streams with the following lines of code:
origin.close();
out.close();
-
1