Improved the Image Downloader (now Zip works at the end of download!)

This commit is contained in:
valfieri
2019-08-22 04:26:38 +02:00
parent 1b88f7d3f9
commit 2ec4b78a8a
7 changed files with 32 additions and 163 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -7,6 +7,8 @@ import org.jsoup.select.Elements;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.Enumeration;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.CompressionMethod;
import java.io.*;
import java.net.URL;
@@ -501,17 +503,38 @@ public class ImgDownloader {
}
}
}
/*try {
//Zipper appZip = new Zipper(destinationPath + set + "/");
//appZip.generateFileList(new File(destinationPath + set + "/"));
//appZip.zipIt(destinationPath + set + ".zip");
//File setFolder = new File(destinationPath + set + "/");
//File[] filesToZip = setFolder.listFiles();
//SevenZ zip = new SevenZ();
//zip.addFilesToZip(setFolder, new File(destinationPath + set + ".zip"));
try {
try {
File oldzip = new File(destinationPath + "/" + set + "/" + set + ".zip");
oldzip.delete();
} catch (Exception e) {}
ZipParameters zipParameters = new ZipParameters();
zipParameters.setCompressionMethod(CompressionMethod.STORE);
File folder = new File(destinationPath + set + "/");
File[] listOfFile = folder.listFiles();
net.lingala.zip4j.ZipFile zipped = new net.lingala.zip4j.ZipFile(destinationPath + "/" + set + "/" + set + ".zip");
for (int i = 0 ; i < listOfFile.length; i++){
if(listOfFile[i].isDirectory()){
zipped.addFolder(listOfFile[i],zipParameters);
} else {
zipped.addFile(listOfFile[i], zipParameters);
}
}
File destFolder = new File(destinationPath + set + "/");
listOfFiles = destFolder.listFiles();
for(int u = 0; u < listOfFiles.length; u++){
if (!listOfFiles[u].getName().contains(".zip")){
if(listOfFiles[u].isDirectory()){
File[] listOfSubFiles = listOfFiles[u].listFiles();
for(int j = 0; j < listOfSubFiles.length; j++)
listOfSubFiles[j].delete();
}
listOfFiles[u].delete();
}
}
} catch (Exception e) {
e.printStackTrace();
}*/
}
}
return res;
}

View File

@@ -1,74 +0,0 @@
package net.wagic.utils;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.compress.utils.IOUtils;
import java.io.*;
import java.util.Collection;
import java.util.Enumeration;
public class SevenZ {
/**
* Add all files from the source directory to the destination zip file
*
* @param source the directory with files to add
* @param destination the zip file that should contain the files
* @throws IOException if the io fails
* @throws ArchiveException if creating or adding to the archive fails
*/
public void addFilesToZip(File source, File destination) throws IOException, ArchiveException {
OutputStream archiveStream = new FileOutputStream(destination);
ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, archiveStream);
File[] fileList = source.listFiles();
for (int i = 0; i < fileList.length; i++) {
File file = fileList[i];
if(!file.isDirectory()){
String entryName = getEntryName(source, file);
ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
archive.putArchiveEntry(entry);
BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
IOUtils.copy(input, archive);
input.close();
archive.closeArchiveEntry();
} else {
File[] subfileList = file.listFiles();
for (int j = 0; j < subfileList.length; j++) {
File subfile = subfileList[j];
String entryName = getEntryName(source, subfile);
ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
archive.putArchiveEntry(entry);
BufferedInputStream input = new BufferedInputStream(new FileInputStream(subfile));
IOUtils.copy(input, archive);
input.close();
archive.closeArchiveEntry();
}
}
}
archive.finish();
archiveStream.close();
}
/**
* Remove the leading part of each entry that contains the source directory name
*
* @param source the directory where the file entry is found
* @param file the file that is about to be added
* @return the name of an archive entry
* @throws IOException if the io fails
*/
private String getEntryName(File source, File file) throws IOException {
int index = source.getAbsolutePath().length() + 1;
String path = file.getCanonicalPath();
return path.substring(index);
}
}

View File

@@ -1,80 +0,0 @@
package net.wagic.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Zipper {
private List <String> fileList;
private String SOURCE_FOLDER;
public Zipper(String source) {
fileList = new ArrayList < String > ();
SOURCE_FOLDER = source;
}
public void zipIt(String zipFile) {
byte[] buffer = new byte[1024];
String source = new File(SOURCE_FOLDER).getName();
FileOutputStream fos = null;
ZipOutputStream zos = null;
try {
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(fos);
zos.setLevel(ZipOutputStream.STORED);
System.out.println("Output to Zip : " + zipFile);
FileInputStream in = null;
for (String file: this.fileList) {
System.out.println("File Added : " + file);
ZipEntry ze = new ZipEntry(file);
zos.putNextEntry(ze);
try {
in = new FileInputStream(SOURCE_FOLDER + File.separator + file);
int len;
while ((len = in .read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
} finally {
in.close();
}
}
zos.closeEntry();
System.out.println("Folder successfully compressed");
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void generateFileList(File node) {
// add file only
if (node.isFile()) {
fileList.add(generateZipEntry(node.toString()));
}
if (node.isDirectory()) {
String[] subNote = node.list();
for (String filename: subNote) {
generateFileList(new File(node, filename));
}
}
}
private String generateZipEntry(String file) {
return file.substring(SOURCE_FOLDER.length(), file.length());
}
}