1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.testUtil;
15
16 import java.io.BufferedReader;
17 import java.io.File;
18 import java.io.FileInputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.util.Enumeration;
23 import java.util.List;
24 import java.util.zip.GZIPInputStream;
25 import java.util.zip.ZipEntry;
26 import java.util.zip.ZipFile;
27
28 public class FileToBufferUtil {
29
30 static public void readIntoList(File file, List<String> stringList) throws IOException {
31
32 if (file.getName().endsWith(".gz")) {
33 gzFileReadIntoList(file, stringList);
34 } else if (file.getName().endsWith(".zip")) {
35 zipFileReadIntoList(file, stringList);
36 } else {
37 regularReadIntoList(file, stringList);
38 }
39 }
40
41 private static void zipFileReadIntoList(File file, List<String> stringList) throws IOException {
42 System.out.println("Reading zip file [" + file + "]");
43 try (ZipFile zipFile = new ZipFile(file)) {
44 Enumeration<? extends ZipEntry> entries = zipFile.entries();
45 ZipEntry entry = entries.nextElement();
46 readInputStream(zipFile.getInputStream(entry), stringList);
47 }
48 }
49
50 static void readInputStream(InputStream is, List<String> stringList) throws IOException {
51 BufferedReader in = new BufferedReader(new InputStreamReader(is));
52 String line;
53 while ((line = in.readLine()) != null) {
54 stringList.add(line);
55 }
56 in.close();
57 }
58
59 static public void regularReadIntoList(File file, List<String> stringList) throws IOException {
60 FileInputStream fis = new FileInputStream(file);
61 BufferedReader in = new BufferedReader(new InputStreamReader(fis));
62 String line;
63 while ((line = in.readLine()) != null) {
64 stringList.add(line);
65 }
66 in.close();
67 }
68
69 static public void gzFileReadIntoList(File file, List<String> stringList) throws IOException {
70 FileInputStream fis = new FileInputStream(file);
71 GZIPInputStream gzis = new GZIPInputStream(fis);
72 readInputStream(gzis, stringList);
73 }
74
75 }