3516fd20af2155ee7f54483c3bc7b2ba877371ed
1 package be
.nikiroo
.utils
;
3 import java
.io
.BufferedReader
;
5 import java
.io
.FileInputStream
;
6 import java
.io
.FileOutputStream
;
7 import java
.io
.FileReader
;
8 import java
.io
.FileWriter
;
9 import java
.io
.IOException
;
10 import java
.io
.InputStream
;
11 import java
.io
.OutputStream
;
12 import java
.util
.stream
.Stream
;
13 import java
.util
.zip
.ZipEntry
;
14 import java
.util
.zip
.ZipOutputStream
;
17 * This class offer some utilities based around {@link Stream}s.
21 public class IOUtils
{
23 * Write the data to the given {@link File}.
28 * the target {@link File}
31 * in case of I/O error
33 public static void write(InputStream in
, File target
) throws IOException
{
34 OutputStream out
= new FileOutputStream(target
);
43 * Write the data to the given {@link OutputStream}.
48 * the target {@link OutputStream}
51 * in case of I/O error
53 public static void write(InputStream in
, OutputStream out
)
55 byte buffer
[] = new byte[4069];
56 for (int len
= 0; (len
= in
.read(buffer
)) > 0;) {
57 out
.write(buffer
, 0, len
);
62 * Recursively Add a {@link File} (which can thus be a directory, too) to a
63 * {@link ZipOutputStream}.
68 * the path to prepend to the ZIP info before the actual
71 * the source {@link File} (which can be a directory)
73 * FALSE if we need to add a {@link ZipEntry} for base/target,
74 * TRUE to add it at the root of the ZIP
77 * in case of I/O error
79 public static void zip(ZipOutputStream zip
, String base
, File target
,
80 boolean targetIsRoot
) throws IOException
{
81 if (target
.isDirectory()) {
83 if (base
== null || base
.isEmpty()) {
84 base
= target
.getName();
86 base
+= "/" + target
.getName();
88 zip
.putNextEntry(new ZipEntry(base
+ "/"));
90 for (File file
: target
.listFiles()) {
91 zip(zip
, base
, file
, false);
94 if (base
== null || base
.isEmpty()) {
95 base
= target
.getName();
97 base
+= "/" + target
.getName();
99 zip
.putNextEntry(new ZipEntry(base
));
100 FileInputStream in
= new FileInputStream(target
);
102 IOUtils
.write(in
, zip
);
110 * Zip the given source into dest.
113 * the source {@link File} (which can be a directory)
115 * the destination <tt>.zip</tt> file
117 * FALSE if we need to add a {@link ZipEntry} for src, TRUE to
118 * add it at the root of the ZIP
120 * @throws IOException
121 * in case of I/O error
123 public static void zip(File src
, File dest
, boolean srcIsRoot
)
125 OutputStream out
= new FileOutputStream(dest
);
127 ZipOutputStream zip
= new ZipOutputStream(out
);
129 IOUtils
.zip(zip
, "", src
, srcIsRoot
);
139 * Write the {@link String} content to {@link File}.
142 * the directory where to write the {@link File}
144 * the {@link File} name
148 * @throws IOException
149 * in case of I/O error
151 public static void writeSmallFile(File dir
, String filename
, String content
)
157 FileWriter writerVersion
= new FileWriter(new File(dir
, filename
));
159 writerVersion
.write(content
);
161 writerVersion
.close();
166 * Read the whole {@link File} content into a {@link String}.
171 * @return the content
173 * @throws IOException
174 * in case of I/O error
176 public static String
readSmallFile(File file
) throws IOException
{
177 BufferedReader reader
= new BufferedReader(new FileReader(file
));
179 StringBuilder builder
= new StringBuilder();
180 for (String line
= reader
.readLine(); line
!= null; line
= reader
182 builder
.append(line
);
184 return builder
.toString();
191 * Recursively delete the given {@link File}, which may of course also be a
194 * Will silently continue in case of error.
197 * the target to delete
199 public static void deltree(File target
) {
200 for (File file
: target
.listFiles()) {
201 if (file
.isDirectory()) {
204 if (!file
.delete()) {
205 System
.err
.println("Cannot delete file: "
206 + file
.getAbsolutePath());
211 if (!target
.delete()) {
212 System
.err
.println("Cannot delete file: "
213 + target
.getAbsolutePath());