When I zip files using 64 bit Java, the zip file show up as empty on Windows 7. Is there a way to force 32 bit version of zip using 64 bit Java?
EDIT:
I'm able to open/unzip it using third party tools like BeyondCompare. When I browse that file through Windows 7, the content is empty. Unzipping it using Win 7 gives me 'Invalid zip file' error.
public static void zipFiles(Collection<String> fileLocations, String targetZipFile){
if( fileLocations == null || fileLocations.size() <= 0 ){
return;
}
byte[] buffer = new byte[1024];
try{
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(targetZipFile));
FileInputStream in = null;
String fileName = null;
for( String fileLoc : fileLocations ){
fileName = fileLoc.substring(fileLoc.lastIndexOf(File.separator));
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(fileName));
in = new FileInputStream(fileLoc);
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
// Complete the ZIP file
out.close();
}catch(IOException ioe){
LOGGER.error(ioe.getMessage(),ioe);
}
}
Related
I have split zip files that I'm trying to merge using Java. But I get Unexpected end of ZLIB input stream error. Any thoughts on what I'm doing wrong?
File bigZip = new File("bigZip.zip");
List<String> zipList = Arrays.asList("src/14thmayreceipts.zip.001","src/14thmayreceipts.zip.002", "src/14thmayreceipts.zip.003");
Collections.sort(zipList);
ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(bigZip));
for (String entry : zipList) {
readWriteZip(outputStream, entry);
}
outputStream.close();
}
private static void readWriteZip(ZipOutputStream out, String fileName) throws IOException, EOFException {
File file = new File(fileName);
ZipInputStream inStream = new ZipInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024];
int len = 0;
for (ZipEntry e; (e = inStream.getNextEntry()) != null; ) {
out.putNextEntry(e);
while ((len = inStream.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
}
inStream.close();
}
JDK's zip does not support split zip files. And moreover you cannot work with Input/Outpustreams when working with split zip files. And also when merging a split zip files a lot of headers in the zip file have to be updated. zip4j supports such a feature. Sample code:
ZipFile zipFile = new ZipFile("splitZipFileThatHasToBeMerged.zip");
zipFile.mergeSplitZipFiles("mergedOutputZipFile.zip");
I´m trying to implement some "over the air" update mechanism for OSGi bundles. For that, I need to be able to create a jar file from a String (basically the content of the jar file read by JarInputStream). The following example code should illustrate my needs:
//read bundle to be copied!
File originalFile = new File(
"/Users/stefan/Documents/Projects/OSGi/SimpleBundle_1.0.0.201404.jar");
JarInputStream fis = new JarInputStream(new FileInputStream(originalFile));
StringBuilder stringBuilder = new StringBuilder();
int ch;
while ((ch = fis.read()) != -1) {
stringBuilder.append((char) ch);
}
fis.close();
//Create content string
String content = stringBuilder.toString();
if (logger.isInfoEnabled()) {
logger.info(content);
}
//Init new jar input stream
JarInputStream jarInputStream = new JarInputStream(
new ByteArrayInputStream(content.getBytes()));
if (logger.isInfoEnabled()) {
logger.info("Save content to disc!");
}
File newFile = new File(
"/Users/stefan/Documents/Projects/OSGi/equinox/SimpleBundle_1.0.0.201404.jar");
//Init new jar output stream
JarOutputStream fos = new JarOutputStream(
new FileOutputStream(newFile));
if (!newFile.exists()) {
newFile.createNewFile();
}
int BUFFER_SIZE = 10240;
byte buffer[] = new byte[BUFFER_SIZE];
while (true) {
int nRead = jarInputStream.read(buffer, 0,
buffer.length);
if (nRead <= 0)
break;
fos.write(buffer, 0, nRead);
}
//Write content to new jar file.
fos.flush();
fos.close();
jarInputStream.close();
Unfortunately, the created jar file is empty and throws an "Invalid input file" error if I try to open it with JD-GUI. Is it possible to create a jar file from the String "content"?
Best regards and thank you very much
Stefan
Your jar is empty because you do not read anything from the JarInputStream. If you want to read JarInputStream, you should iterate its entries. If you want to change the Manifest, the first entry should be skipped, use the getManifest() of the jarInputStream and the constructor of the JarOutputStream, where Manifest can be specified. Based on your code (no manifest change but plain jar copy):
ZipEntry zipEntry = jarInputStream.getNextEntry();
while (zipEntry != null) {
fos.putNextEntry(zipEntry);
// Simple stream copy comes here
int BUFFER_SIZE = 10240;
byte buffer[] = new byte[BUFFER_SIZE];
int l = jarInputStream.read(buffer);
while(l >= 0) {
fos.write(buffer, 0, l);
l = jarInputStream.read(buffer);
}
zipEntry = jarInputStream.getNextEntry();
}
You only need this if you want to change the content (Manifest or entries) of the JAR file during the copy. Otherwise, simple InputStream and FileOutputStream will do the work (as Tim said).
I need to replace a specific CSS file inside zip archive the file is stored under folders EPUB/styles/stylesheet.css
here is my code(I need to do it in java 6)
public static void main(String[] args) throws IOException {
File newCss=new File("D:\\test\\css\\stylesheet.css");
ZipOutputStream out = new ZipOutputStream(new FileOutputStream
(new File("D:\\test\\css\\edited\\my_book.epub"),true));
out.putNextEntry(new ZipEntry("EPUB/styles/stylesheet.css"));
InputStream in = new FileInputStream(newCss);
byte[] buf = new byte[4096 * 1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
out.close();
System.out.println("Done Replacing entry");
}
}
but on executing this code the whole zip contains only the CSS i replaced all the other contents are lost but the zip shows the same size as before, on extracting the zip i get only the file i replaced.
I have a list of files from different locations. I create a zip file using the following the code which works without error. But when I try to unzip the file in Windows using Extract All it fails seeing unable to find any bytes, yet if I double click into the zip file itself with Windows Explorer I can see the files and individual ones can be opened and contains the correct data
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
for (File next : files)
{
ZipEntry zipEntry = new ZipEntry(next.getName());
zos.putNextEntry(zipEntry);
FileInputStream in = new FileInputStream(next);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
{
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
zos.close();
This may or may not be related but I've found using fixed byte length can lead to a loss of new line characters.
This may help:
final byte[] newLine = System.getProperty(
"line.separator").getBytes("UTF-8");
while ((line = in.readLine()) != null)
final byte[] buffer = line.getBytes("UTF-8");
out.write(buffer, 0, buffer.length);
out.write(newLine, 0, newLine.length);
}
I am sending a request XML to the URL and receiving a zip file to the given path.
Sometimes I'm facing troubles when the bandwidth is low this zip file, most likely 120MB size is not getting downloaded properly. And getting an error when extracting the zip file. Extracting happens from the code as well. When I download in high bandwidth this file gets download without issue.
I'm looking for a solution without making the bandwidth high, from program level are there any ways to download this zip file, may be part by part or something like that? Or anyother solution that you all are having is highly appreciated.
Downloading :
url = new URL(_URL);
sc = (HttpURLConnection) url.openConnection();
sc.setDoInput(true);
sc.setDoOutput(true);
sc.setRequestMethod("POST");
sc.connect();
OutputStream mOstr = sc.getOutputStream();
mOstr.write(request.getBytes());
InputStream in = sc.getInputStream();
FileOutputStream out = new FileOutputStream(path);
int count;
byte[] buffer = new byte[86384];
while ((count = in.read(buffer,0,buffer.length)) > 0)
out.write(buffer, 0, count);
out.close();
Extracting :
try {
ZipFile zipFile = new ZipFile(path+zFile);
Enumeration<?> enu = zipFile.entries();
while (enu.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) enu.nextElement();
String name = path+"/data_FILES/"+zipEntry.getName();
long size = zipEntry.getSize();
long compressedSize = zipEntry.getCompressedSize();
System.out.printf("name: %-20s | size: %6d | compressed size: %6d\n", name, size, compressedSize);
File file = new File(name);
if (name.endsWith("/")) {
file.mkdirs();
continue;
}
File parent = file.getParentFile();
if (parent != null) {
parent.mkdirs();
}
InputStream is = zipFile.getInputStream(zipEntry);
FileOutputStream fos = new FileOutputStream(file);
byte[] bytes = new byte[86384];
int length;
while ((length = is.read(bytes)) >= 0) {
fos.write(bytes, 0, length);
}
is.close();
fos.close();
}
zipFile.close();
} catch (Exception e) {
log("Error in extracting zip file ");
e.printStackTrace();
}