org.jlot.client.remote.support.ResourceZipper Maven / Gradle / Ivy
package org.jlot.client.remote.support;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.inject.Inject;
import org.jlot.client.configuration.ProjectConfiguration;
import org.springframework.stereotype.Component;
@Component
public class ResourceZipper
{
@Inject
private ProjectConfiguration projectConfiguration;
public File zip ( Set filenames )
{
try
{
File tempFile = File.createTempFile("jlot", ".zip");
FileOutputStream fos = new FileOutputStream(tempFile);
ZipOutputStream zos = new ZipOutputStream(fos);
for (String filename : filenames)
{
ZipEntry ze = new ZipEntry(filename);
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(projectConfiguration.getBasedir() + filename);
byte[] data = new byte[2048];
int b = -1;
while ( ( b = in.read(data) ) != -1)
{
zos.write(data, 0, b);
}
in.close();
zos.closeEntry();
}
zos.close();
return tempFile;
}
catch (IOException e)
{
throw new IllegalStateException("cant build zip file", e);
}
}
}