简体   繁体   中英

compress list of json obects to gzip file in JAVA

I want to convert ist of JSON objects to gzip file using JAVA

Below is my JSON file

[
    {
        "id": "01",
        "Status": "Open",
        "siteId": "01",
        "siteName": "M1"
    },
    {
        "id": "02",
        "Status": "Open",
        "siteId": "02",
        "siteName": "M2"
    },
    {
        "id": "03",
        "Status": "Open",
        "siteId": "03",
        "siteName": "M3"
    }
]

Code written till now:

public static void main(String args[]) throws IOException
{
    final ObjectMapper objectMapper = new ObjectMapper();
    String fileName = "jsonList.json";
    ClassLoader classLoader = className.class.getClassLoader();
    File file = new File(classLoader.getResource(fileName).getFile());
    System.out.println("File Found : " + file.exists());
    List<document> list = objectMapper.readValue(file,new TypeReference<List<document>>(){});
    System.out.println("List of json objects");
    //code to compress list of json objects (list)
}

Document class

public class document {
    public String id;
    public String status;
   public String sideId;
   public String siteName;

}

Please suggest me the code to compress the list Thanks!

You could use something like this, StudentL:

    OutputStream outputStream = new FileOutputStream("output_file.zip");
    GZIPOutputStream gzipOuputStream = new GZIPOutputStream(outputStream);
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(gzipOuputStream);

    for (Document doc : list) {
        objectOutputStream.writeObject(doc);
    }

    objectOutputStream.close();

I guess this is for an exercise, otherwise consider that what you are doing does not make so much sense. You don't need to read the JSON and do the whole unmarshalling thing; you can simply take the existing file and gzip it.

Bonus edit: there are two things in your code that tend to irritate Java developers a lot. The first is that in Java the names of classes are usually written in Camel case . The second is that you don't use a new line character before the curly braces, as you would in C/C++ and other languages.

Wrong:

public class Whatever 
{

Right:

public class Whatever {

See you around!

Here is what I think you can do:

List<Map<String, String>> jsonList = ...;
 
// Create a FileOutputStream
FileOutputStream fileOutputStream 
    = new FileOutputStream("/path/to/file");
 
// Wrap the FileOutputStream in a BufferedOutputStream
// so that we write in larger chunks for performance
BufferedOutputStream bufferedOutputStream 
    = new BufferedOutputStream(fileOutputStream);
 
// Wrap the BufferedOutputStream in a GIZPOutputStream
// so that when we write to the gzipOutputStream,
// we first compress then buffer then write to the file.
GZIPOutputStream compressedOutputStream 
    = new GZIPOutputStream(bufferedOutputStream);
 
// Write the JSON map into the compressedOutputStream
objectMapper.writeValue(compressedOutputStream, jsonList);

Here is the Jackson Library you can refer to; And here is a detailed explanation of compress object to a file using object mapper and OutputStreams.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM