Browse Source

用IOUtils.zip压缩文件

research/10.0
1 6 years ago
parent
commit
57283ecd99
  1. 23
      designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/entity/FileEntityBuilder.java

23
designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/entity/FileEntityBuilder.java

@ -18,14 +18,12 @@ import com.fr.third.org.apache.http.entity.mime.content.FileBody;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.zip.ZipEntry;
/** /**
* @author alex sung * @author alex sung
@ -83,24 +81,9 @@ public class FileEntityBuilder {
public File generateZipFile(String pathName) { public File generateZipFile(String pathName) {
File zipFile = null; File zipFile = null;
try { try {
File file = new File(pathName);
zipFile = new File(pathName + ".zip"); zipFile = new File(pathName + ".zip");
InputStream input = null; java.util.zip.ZipOutputStream zipOut = new java.util.zip.ZipOutputStream(new FileOutputStream(zipFile));
java.util.zip.ZipOutputStream zipOut = null; IOUtils.zip(zipOut, new File(pathName));
zipOut = new java.util.zip.ZipOutputStream(new FileOutputStream(zipFile));
int temp = 0;
if (file.isDirectory()) {
File lists[] = file.listFiles();
for (int i = 0; i < lists.length; i++) {
input = new FileInputStream(lists[i]);
zipOut.putNextEntry(new ZipEntry(file.getName()
+ File.separator + lists[i].getName()));
while ((temp = input.read()) != -1) {
zipOut.write(temp);
}
input.close();
}
}
zipOut.close(); zipOut.close();
} catch (Exception e) { } catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e); FineLoggerFactory.getLogger().error(e.getMessage(), e);
@ -159,7 +142,7 @@ public class FileEntityBuilder {
return data.optString("url"); return data.optString("url");
} }
} catch (JSONException e) { } catch (JSONException e) {
FineLoggerFactory.getLogger().error("Illegal response text."); FineLoggerFactory.getLogger().error("Illegal response text."+e, e.getMessage());
} }
return null; return null;
} }

Loading…
Cancel
Save