Initial commit, version 0.9.2
[fanfix.git] / src / be / nikiroo / utils / IOUtils.java
CommitLineData
ec1f3444
NR
1package be.nikiroo.utils;
2
3import java.io.BufferedReader;
4import java.io.File;
5import java.io.FileInputStream;
6import java.io.FileOutputStream;
7import java.io.FileReader;
8import java.io.FileWriter;
9import java.io.IOException;
10import java.io.InputStream;
11import java.io.OutputStream;
12import java.util.stream.Stream;
13import java.util.zip.ZipEntry;
14import java.util.zip.ZipOutputStream;
15
16/**
17 * This class offer some utilities based around {@link Stream}s.
18 *
19 * @author niki
20 */
21public class IOUtils {
22 /**
23 * Write the data to the given {@link File}.
24 *
25 * @param in
26 * the data source
27 * @param target
28 * the target {@link File}
29 *
30 * @throws IOException
31 * in case of I/O error
32 */
33 public static void write(InputStream in, File target) throws IOException {
34 OutputStream out = new FileOutputStream(target);
35 try {
36 write(in, out);
37 } finally {
38 out.close();
39 }
40 }
41
42 /**
43 * Write the data to the given {@link OutputStream}.
44 *
45 * @param in
46 * the data source
47 * @param target
48 * the target {@link OutputStream}
49 *
50 * @throws IOException
51 * in case of I/O error
52 */
53 public static void write(InputStream in, OutputStream out)
54 throws IOException {
55 byte buffer[] = new byte[4069];
56 for (int len = 0; (len = in.read(buffer)) > 0;) {
57 out.write(buffer, 0, len);
58 }
59 }
60
61 /**
62 * Recursively Add a {@link File} (which can thus be a directory, too) to a
63 * {@link ZipOutputStream}.
64 *
65 * @param zip
66 * the stream
67 * @param base
68 * the path to prepend to the ZIP info before the actual
69 * {@link File} path
70 * @param target
71 * the source {@link File} (which can be a directory)
72 * @param targetIsRoot
73 * FALSE if we need to add a {@link ZipEntry} for base/target,
74 * TRUE to add it at the root of the ZIP
75 *
76 * @throws IOException
77 * in case of I/O error
78 */
79 public static void zip(ZipOutputStream zip, String base, File target,
80 boolean targetIsRoot) throws IOException {
81 if (target.isDirectory()) {
82 if (!targetIsRoot) {
83 if (base == null || base.isEmpty()) {
84 base = target.getName();
85 } else {
86 base += "/" + target.getName();
87 }
88 zip.putNextEntry(new ZipEntry(base + "/"));
89 }
90 for (File file : target.listFiles()) {
91 zip(zip, base, file, false);
92 }
93 } else {
94 if (base == null || base.isEmpty()) {
95 base = target.getName();
96 } else {
97 base += "/" + target.getName();
98 }
99 zip.putNextEntry(new ZipEntry(base));
100 FileInputStream in = new FileInputStream(target);
101 try {
102 IOUtils.write(in, zip);
103 } finally {
104 in.close();
105 }
106 }
107 }
108
109 /**
110 * Zip the given source into dest.
111 *
112 * @param src
113 * the source {@link File} (which can be a directory)
114 * @param dest
115 * the destination <tt>.zip</tt> file
116 * @param srctIsRoot
117 * FALSE if we need to add a {@link ZipEntry} for src, TRUE to
118 * add it at the root of the ZIP
119 *
120 * @throws IOException
121 * in case of I/O error
122 */
123 public static void zip(File src, File dest, boolean srcIsRoot)
124 throws IOException {
125 OutputStream out = new FileOutputStream(dest);
126 try {
127 ZipOutputStream zip = new ZipOutputStream(out);
128 try {
129 IOUtils.zip(zip, "", src, srcIsRoot);
130 } finally {
131 zip.close();
132 }
133 } finally {
134 out.close();
135 }
136 }
137
138 /**
139 * Write the {@link String} content to {@link File}.
140 *
141 * @param dir
142 * the directory where to write the {@link File}
143 * @param filename
144 * the {@link File} name
145 * @param content
146 * the content
147 *
148 * @throws IOException
149 * in case of I/O error
150 */
151 public static void writeSmallFile(File dir, String filename, String content)
152 throws IOException {
153 if (!dir.exists()) {
154 dir.mkdirs();
155 }
156
157 FileWriter writerVersion = new FileWriter(new File(dir, filename));
158 try {
159 writerVersion.write(content);
160 } finally {
161 writerVersion.close();
162 }
163 }
164
165 /**
166 * Read the whole {@link File} content into a {@link String}.
167 *
168 * @param file
169 * the {@link File}
170 *
171 * @return the content
172 *
173 * @throws IOException
174 * in case of I/O error
175 */
176 public static String readSmallFile(File file) throws IOException {
177 BufferedReader reader = new BufferedReader(new FileReader(file));
178 try {
179 StringBuilder builder = new StringBuilder();
180 for (String line = reader.readLine(); line != null; line = reader
181 .readLine()) {
182 builder.append(line);
183 }
184 return builder.toString();
185 } finally {
186 reader.close();
187 }
188 }
189
190 /**
191 * Recursively delete the given {@link File}, which may of course also be a
192 * directory.
193 * <p>
194 * Will silently continue in case of error.
195 *
196 * @param target
197 * the target to delete
198 */
199 public static void deltree(File target) {
200 for (File file : target.listFiles()) {
201 if (file.isDirectory()) {
202 deltree(file);
203 } else {
204 if (!file.delete()) {
205 System.err.println("Cannot delete file: "
206 + file.getAbsolutePath());
207 }
208 }
209 }
210
211 if (!target.delete()) {
212 System.err.println("Cannot delete file: "
213 + target.getAbsolutePath());
214 }
215 }
216}