[JAVA] Implementation that creates a temporary file, compresses it, does some processing, and deletes it

Since I investigated how to create temporary files and implement zip compression in Java, I will write an article for the purpose of organizing. "Some processing" was supposed to be uploaded somewhere.

Sample.java


//Create a temporary file
File tmpFile = File.createTempFile("tmpSqlFile", ".txt");
		
//Data writing
writeDate(tmpFile);
		
//compression
compress(tmpFile);
		
//Some processing
		
//File deletion
deleteFile(tmpFile);
deleteFile(new File(getZipPath(tmpFile)));

By saying File.createTempFile ("tmpSqlFile", ".txt") , I assumed a text file in which SQL was written.

Sample.java


private static void writeDate(File f) throws IOException {

	//FileWriter class creation
	FileWriter fw = new FileWriter(f);
		
	//writing
	getList().stream().forEach(s -> {		
		try {
			fw.write(s + ";\r\n");
		} catch (IOException e) {
			e.printStackTrace();
		}
	});
		
	//Close file
	fw.close();
}

Sample.java


private static List<String> getList() {
		
	//Data preparation
	List<String> arr = new ArrayList<>();
	arr.add("UPDATE uesrs SET name = 'newName1' WHERE id = 0001;");
	arr.add("UPDATE uesrs SET name = 'newName2' WHERE id = 0002;");

	Map<String, List<String>> map = new HashMap<>();
	map.put("users", arr);
		
	//SQL extraction
	List<String> sqlList = new ArrayList<>();
		
	map.values().stream().forEach(a -> {
		a.stream().forEach(sql -> sqlList.add(sql));
	});
		
	return sqlList;	
}

I was thinking of a pattern in which a map is generated as data, with the table name as the key and the value being a list of UPDATE statements for that table. The above is for one table, but in consideration of multiple tables, forEach is a nested implementation.

Sample.java


private static void compress(File f) throws IOException {
		
	ZipOutputStream zos = 
			new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(getZipPath(f))));
	ZipEntry entry = new ZipEntry(f.getName());
	zos.putNextEntry(entry);
	InputStream is = new BufferedInputStream(new FileInputStream(f));
		
	int len = 0;
	byte[] buf = new byte[1024];
	while ((len = is.read(buf)) != -1) {
		zos.write(buf, 0, len);
	}
		
	zos.close();
	is.close();
}

Only zip the argument File object (not multiple files).

Sample.java


private static String getZipPath(File f) {
	return f.getParent() + "\\hoge.zip"; 
}

A zip file is also generated in the same layer where the temporary file was created.

Sample.java


private static void deleteFile(File f) {
	if (f != null && f.exists()) {
		f.delete();
	}
}

There is a scene to implement with reference to the above implementation, and if you review it again, there may be improvements. If you think about the necessity of creating a temporary file in the first place.

Recommended Posts

Implementation that creates a temporary file, compresses it, does some processing, and deletes it
21 Load the script from a file and execute it
Test a class that takes a Context and reads / writes a file