Java Writing all pdf files bytes in .dat file - java

I wrote the code write all pdf files in folder get bytes and write in .dat file.. Acutally its working and writing all bytes in .dat file but When I open that .dat file with Acrobat it open with black page.
Here is my code..
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
public class xmlfile1filebytes {
public static void main(String[] args) throws IOException {
File folder = new File ("07072013");
File[] listOfFiles = folder.listFiles();
System.out.println("There are " + listOfFiles.length + " files");
String filesin;
String timeStamp = new SimpleDateFormat("MM-dd-yyyy[HH.mm.ss]")
.format(Calendar.getInstance().getTime());
System.out.println(timeStamp);
BufferedWriter xmlfile = null;
BufferedWriter datfile = null;
String outxmlfile = ("07072013\\" + timeStamp + ".xml");
xmlfile = new BufferedWriter(new FileWriter(outxmlfile));
String outdatfile = ("07072013\\" + timeStamp + ".dat");
datfile = new BufferedWriter(new FileWriter(outdatfile));
int offset = 0;
int size = 0;
for (int i = 0; i < listOfFiles.length; i++) {
File f = listOfFiles[i];
// System.out.println(i + " " + f.getAbsolutePath());
if (f.isFile()) {
filesin = listOfFiles[i].getName();
if (filesin.endsWith("pdf")) {
Path aPath = Paths.get(f.getAbsolutePath());
System.out.println(filesin);
byte[] actualBytes = Files.readAllBytes(aPath);
size = actualBytes.length;
xmlfile.append((i + 1) + ")" + " File = " + filesin + ", Offset = " + offset + ", Size = " + size + "\n");
offset = offset + size;
xmlfile.newLine();
String s = new String(actualBytes);
datfile.append(s);
datfile.newLine();
File datfileinfolder = new File ("07072013\\" + timeStamp + ".dat");
long datfilesize = datfileinfolder.length();
final int BLOCK_SIZE = 200 * 1024;
for (int curBlock = 0; curBlock < actualBytes.length; curBlock += BLOCK_SIZE) {
String toWrite = new String(
Arrays.copyOfRange(actualBytes, curBlock, Math.min(curBlock + BLOCK_SIZE, actualBytes.length)));
String suffix = "";
if (curBlock > 0) {
//append underscores other file information and then perform writes
suffix = String.valueOf(curBlock / BLOCK_SIZE);
}
BufferedWriter datfile1 = null;
String outdatfile1 = ("07072013\\" + suffix + timeStamp + ".dat");
datfile1 = new BufferedWriter(new FileWriter(outdatfile1));
datfile1.append(toWrite);
datfile1.close();
}
//long datfilesizeinkb = datfilesize /1024;
//System.out.println("Size = " + datfilesizeinkb);
}
}
}
datfile.close();
xmlfile.close();
}
}

It's unclear from your post and your comments what you're really trying to accomplish. Your original question seemed to be about merging multiple PDF files into a single .dat file, which you expected to be able to open with acrobat.
If that's what you're trying to do, then I suggest using Apache PDFBox and in particular the PDFMergerUtility class. An outline of the code would be like this:
PDFMergerUtility merger = new PDFMergerUtility();
File[] files = folder.listFiles();
for (File file : files) {
merger.addSource(file);
}
merger.setDestinationFileName("output.pdf");
merger.mergeDocuments();
That should combine your source files into a single large PDF file. You could, of course, use a .dat extension on this file, but I'm not sure why you would do so. The only thing that would accomplish is to break the file extension association so double-clicking the file wouldn't open it.
The second question you were asking was how you break the data into 200KB chunks. I'm unsure why you want do do this. If you do this, you will not (necessarily) be able to open the resulting files in Acrobat. PDF files are pretty specific about their internal format. Partial files will not open. If the goal is to have one output file for each input file, then a simple file copy would accomplish this. If the goal is to take all of these files and merge them into a single stream in 200KB chunks (again, why?), then you might want to consider using a compression library instead. In that case, this answer may get you started.

Related

File already existing - repeated name

i am creating a file and when i create that file, i check if it already exists. If it already exists, i want to create it with the same name, but with the (1) after it. I am able to do that and here is the code :
File apkReceived = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"+receivedApkName + ".apk");
if(apkReceived.exists()){
apkReceived=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"+receivedApkName + "(1)"+".apk");
}
byte[] buffer = new byte [8192];
FileOutputStream fos=new FileOutputStream(apkReceived);
then it would continue... (i am writing things on the file).
This works but the problem is that in this situation :
FileTest.apk
FileTest(1).apk
If I receive another Filetest, it will sub my FileTest(1), since it will create it again.
A solution for this would be to check if the file exists again, but then i would have to be doing that for ever.
My goal would be to create (1) and then (2) , etc.
Does any one of you know how to do this ?
EDIT: Obviously i could use a cicle to check it. The problem is on how to get the (1) and then the (2) and don't get the (1)(2)
To avoid reinventing the wheel I suggest using Timestamp it hardly ever will have collisions.
java.util.Date date= new java.util.Date();
Timestamp tstamp = new Timestamp(date.getTime());
File apkReceived = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"+receivedApkName + tstamp + ".apk");
Do Something like this
File apkReceived = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"+receivedApkName + ".apk");
if(apkReceived.exists()){
int new_int_postfix;
//Below _MAX is max numbers of file eg. _MAX = 100
for(int i = 1; i < _MAX; i++) {
apkReceived = = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"+receivedApkName +"("+ i +")"+".apk");
if(!apkReceived.exists()) {
String []name_without_pre = receivedApkName.split("\\(");
receivedApkName = name_without_pre[0];
new_int_postfix = i;
break;
}
}
apkReceived = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"+receivedApkName + "("+new_int_postfix+")"+".apk");
}
byte[] buffer = new byte [8192];
FileOutputStream fos=new FileOutputStream(apkReceived);
Some pseudocode to get you started:
Fetch a list of all files in the directory
For the one you want to copy: check if you already have one or more copies
If you already have "file_(n)"; use "file_(n+1)" as new filename.
Obviously: you should clarify your requirements on the "maximum" n you want to allow; and what to happen when n copies were created; and another is asked for.
If you only store that one type of file in your directory you can do:
File folder = new File("your/path");
File[] listOfFiles = folder.listFiles();
count the size and size + 1 for your next filename.
you can also separate each file with similiar filename on their own directory.
try this
String filename =Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"+receivedApkName + ".apk";
File f = new File(filename);
String extension = ".apk";
int g = 0;
while(f.exists()) {
int i = f.lastIndexOf('.');
if (i > 0)
{ extension = fileName.substring(i+1); }
f.renameTo(f.getPath() + "\" + (f.getName() + g) + "." + extension)
}

java zip contents of a folder instead of folder & winzip compatibility

UPDATE
For issue #2 regarding the folder, I just replaced ZipEntry ze = new ZipEntry(source + File.separator + file); with ZipEntry ze = new ZipEntry(file);.
Issue remains with WINZIP not able to open the zipped file while unzip can unzip the file. WINZIP's error is: Error: unable to seek to beginning of central directory.
ORIGINAL POST
I have the following code that I have gotten and slightly modified from one of the questions on SO. In my application, I set OUTPUT_ZIP_FILE to /var/tmp/test/test.zip and my source folder as /var/tmp/test.
I have two problems:
1- Winzip does not recognize the zip file while unix unzip does - Not sure if this is due to #2 below
2- when I use unzip to unzip the file, it unzips the whole directory hierarchy: It creates /var/tmp/test inside of /var/tmp/test leading to /var/tmp/test/var/tmp/test and then the files inside that... I only want to zip the files and not the hierarchy...
Any help would be much appreciated!
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 ZipFiles {
List<String> fileList;
String OUTPUT_ZIP_FILE;
String SOURCE_FOLDER;
ZipFiles() {
fileList = new ArrayList<String>();
}
public void zipIt(String ZipFiles) {
byte[] buffer = new byte[1024];
String source = "";
Boolean shouldZip = true;
try {
try {
// System.out.println("ZipFiles::zipIt::SOURCE_FOLDER::" +
// SOURCE_FOLDER);
source = SOURCE_FOLDER.substring(
SOURCE_FOLDER.lastIndexOf("\\") + 1,
SOURCE_FOLDER.length());
// System.out.println("ZipFiles::zipIt::source::" + source);
} catch (Exception e) {
source = SOURCE_FOLDER;
}
for (String file : this.fileList) {
if (file.endsWith("zip")) // This has already been zipped
{
shouldZip = false;
}
}
if (shouldZip) {
FileOutputStream fos = new FileOutputStream(ZipFiles);
ZipOutputStream zos = new ZipOutputStream(fos);
// System.out.println("Output to Zip : " + ZipFiles);
for (String file : this.fileList) {
// System.out.println("File Added : " + file);
ZipEntry ze = new ZipEntry(source + File.separator + file);
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(SOURCE_FOLDER
+ File.separator + file);
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
}
zos.closeEntry();
// remember close it
zos.close();
}
// System.out.println("Folder successfully compressed");
} catch (IOException ex) {
ex.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) {
// System.out.println("ZipFiles::generateZipEntry::file::" + file);
return file.substring(SOURCE_FOLDER.length(), file.length());
}
}
As noted in the update to my question, for issue #2: just replaced ZipEntry ze = new ZipEntry(source + File.separator + file); with ZipEntry ze = new ZipEntry(file);
For issue #1 with winzip, the zip file I was testing was actually being downloaded via my webapp. Problem was in my groovy side of the code actually:
def download = {
def folder = params.folder
def file = new File( folder.toString())
response.setHeader "Content-disposition", "attachment; filename=${file.name}"
response.outputStream << file.text
response.outputStream.flush()
response.outputStream.close()
}
When I replaced outputStream <<file.text with outputStream << file.newInputStream() everything worked correctly.
As for Unix' unzip, I was testing directly on the created file instead of the downloaded file!!!

Java Create new file automatically depend on size [duplicate]

This question already has answers here:
Check file size while writing file and create new file
(2 answers)
Closed 9 years ago.
Trying to Write java Code to write ".dat" File.. While wring check size as well.. every 200 KB create new ".dat" continue to write file. Is there any way that i can do that..
when file more than 200KB and create new one filename like this
1st file name.. 1_filename
2nd file name.. 2_filename
3rd file name.. 3_filename........
or if file create only one file means less than 200KB than
Filenamd.. filename as in code..
I wrote code to write files but.. its only writing 2 files.. and that's not right way to do it..
Here is my code..
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class xmlfile1filebytes {
public static void main(String[] args) throws IOException {
File folder = new File ("07072013");
File[] listOfFiles = folder.listFiles();
System.out.println("There are " + listOfFiles.length + " files");
String filesin;
String timeStamp = new SimpleDateFormat("MM-dd-yyyy[HH.mm.ss]")
.format(Calendar.getInstance().getTime());
System.out.println(timeStamp);
BufferedWriter xmlfile = null;
BufferedWriter datfile = null;
String outxmlfile = ("07072013\\" + timeStamp + ".xml");
xmlfile = new BufferedWriter(new FileWriter(outxmlfile));
String outdatfile = ("07072013\\" + timeStamp + ".dat");
datfile = new BufferedWriter(new FileWriter(outdatfile));
int offset = 0;
int size = 0;
for (int i = 0; i < listOfFiles.length; i++) {
File f = listOfFiles[i];
// System.out.println(i + " " + f.getAbsolutePath());
if (f.isFile()) {
filesin = listOfFiles[i].getName();
if (filesin.endsWith("pdf")) {
Path aPath = Paths.get(f.getAbsolutePath());
System.out.println(filesin);
byte[] actualBytes = Files.readAllBytes(aPath);
size = actualBytes.length;
xmlfile.append((i + 1) + ")" + " File = " + filesin + ", Offset = " + offset + ", Size = " + size + "\n");
offset = offset + size;
xmlfile.newLine();
String s = new String(actualBytes);
datfile.append(s);
datfile.newLine();
File datfileinfolder = new File ("07072013\\" + timeStamp + ".dat");
long datfilesize = datfileinfolder.length();
long datfilesizeinkb = datfilesize /1024;
System.out.println(datfilesizeinkb);
if (datfilesizeinkb >= 200) {
datfile.close();
BufferedWriter datfile1 = null;
String outdatfile1 = ("07072013\\" + "1_"+ timeStamp + ".dat");
datfile1 = new BufferedWriter(new FileWriter(outdatfile1));
String s1 = new String(actualBytes);
datfile1.append(s1);
datfile1.close();
}
}
}
}
xmlfile.close();
}
}
And I get error when write file more than 400 KB..
ERROR:
There are 10 files
07-09-2013[16.03.00]
1192970_eBill_20130709.pdf
96
1321470_eBill_20130709.pdf
208
1724897_eBill_20130709.pdf
Exception in thread "main" java.io.IOException: Stream closed
at java.io.BufferedWriter.ensureOpen(Unknown Source)
at java.io.BufferedWriter.write(Unknown Source)
at java.io.Writer.write(Unknown Source)
at java.io.Writer.append(Unknown Source)
at xmlfile1filebytes.main(xmlfile1filebytes.java:65)
I suggest you create a separate class whose only job it is to create files (in sequence), write data to the file, keep an eye on the size, and close / open files as needed. This will make your code more organized - you will be able to see your mistakes more clearly. The class would have the following methods:
setMax(maxVal) - set the maximum file size (default 400k)
setRoot(rootPath) - folder where files will be created
setName(rootName) - "format string" used to generate file names
(e.g. "myFile%03d.dat")
writeData(dataSize, dataBlock) - write data. If no file open, open it.
If file too big, split it.
closeFile() - flush buffers, close the last file.
Could be part of the destructor
currentFile() - returns name of current file (for debug)
currentSize() - returns current file size (for debug)
If you can figure out how to write this class you will solve your initial problem and have something you can re-use at the same time.
If you want to stay with (mostly) the code structure you already have, then your fix is to remove your current line 85.
83: String s1 = new String(actualBytes);
84: datfile1.append(s1);
85: datfile1.close(); <<<<<<<<<<< remove this line
86: }
Once you have closed the file, your next attempt to write to it will fail - and that's the error you are seeing.
You do datfile.close(), and on the next iteration through the for loop you .append() to it. You'll have to initialize (i.e. open) your datfile inside the for loop.
Okay looks like a book keeping issue.
Here is how it should look like
.
.
.
long datfilesize = datfileinfolder.length();;
final int BLOCK_SIZE = 200 * 1024;
for (int curBlock = 0; curBlock < actualBytes.length; curBlock += BLOCK_SIZE) {
String toWrite = new String(
Arrays.copyOfRange(actualBytes, curBlock, Math.min(curBlock + BLOCK_SIZE, actualBytes.length)
);
String suffix = "";
if (curBlock > 0) {
//append underscores other file information and then perform writes
suffix = String.valueOf(curBlock / BLOCK_SIZE);
}
//Your code more or less
BufferedWriter datfile1 = null;
String outdatfile1 = ("07072013\\" + suffix + timeStamp + ".dat");
datfile1 = new BufferedWriter(new FileWriter(outdatfile1));
datfile1.append(toWrite);
datfile1.close();
}
Edit: Does this make more sense ?
Here's the gist. The for loop iterates in block size of BLOCK_SIZE(200 KB). If file size is less than BLOCK_SIZE, you need not append any suffixes(See the if block). Else you append the suffix and then write to the file. The string is given by the local variable toWrite. You have read the whole file already and stored the bytes in actualBytes. Hope this makes more sense.

Java Code to Zip/FTP Directory in a UNIX Server

I want to write a couple of methods in a Java package, which would be deployed in a UNIX Server.
As of now, my code was for Windows Server, for which I used the following code to zip Directory.
public static final void zipDirectory(File fBatchDirectory, String batchName, String ondemandDocExtension) throws IOException
{
//Set zip file name
File zip = new File(fBatchDirectory + "\\" + StringUtils.replace(batchName,".ind", "") + ".zip");
//filter file
FileFilter filter = new FileFilter(ondemandDocExtension);
File[] files = fBatchDirectory.listFiles(filter);
if(files.length > 0)
{
ZipOutputStream zos = new ZipOutputStream( new FileOutputStream(zip) );
zip(files, fBatchDirectory, zos , ondemandDocExtension);
zos.close();
}
}
private static final void zip(File[] files, File base,ZipOutputStream zos , String docExtension) throws IOException
{
byte[] buffer = new byte[8192];
int read = 0;
for (int i = 0, n = files.length; i < n; i++)
{
//Add to zip only if its file
if (files[i].isFile())
{
FileInputStream in = new FileInputStream(files[i]);
ZipEntry entry = new ZipEntry(files[i].getPath().substring(base.getPath().length() + 1));
zos.putNextEntry(entry);
while (-1 != (read = in.read(buffer)))
{
zos.write(buffer, 0, read);
}
in.close();
}
}
}
I am confused as to how to replicate the same functionality to zip Directory in Java, for UNIX?
And then I want to FTP the files from a UNIX Serve to another UNIX Server.
Any pointers would be greatly appreciated.
At a first glance, the only problem I see is at this line:
File zip = new File(fBatchDirectory + "\\" + StringUtils.replace(batchName,".ind", "") + ".zip");
Because you are explicitly using the double backslash (\\) in your filename. If you change that for File.separator your code should work for both operating systems:
File zip = new File(fBatchDirectory + File.separator + StringUtils.replace(batchName,".ind", "") + ".zip");
For the FTP part of it, you can get down and dirty and use an FTP client or use a more high level library like Apache Commons VFS which, by the way, inspired the new IO FileSystem API in Java 7, but I don't now about any library implementing the FTP protocol with the new API at the moment.

how to create java zip archives with a max file size limit

I need to write an algorithm in java (for an android app) to read a folder containing more folders and each of those containing images and audio files so the structure is this: mainDir/categorySubfolder/myFile1.jpg
My problem is that I need to limit the size of the archive to 16mb and at runtime, create as many archives as needed to contain all my files from my main mainDir folder.
I tried several examples from the net and I read the java documentation but I can't manage to understand and put it all together the way I need it. Has someone done this before or has a link or an example for me?
I resolved the reading of the files with a recursive method but I can't write the logic for the zip creation.
I'm open for suggestions or better a working example.
zip4j is a great library that can create multi-part zip files.
net.lingala.zip4j.core.ZipFile zipFile = new ZipFile("out.zip");
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
zipFile.createZipFileFromFolder("path/to/source/dir", parameters, true, maximum size);
You can find more examples on their web-site.
I am using below code/class to split and zip a large amount/size of files.
I have tested this class on below
number of uncompressed files : 116
total size (uncompressed) : 29.1 GB
ZIP file size limit (each) : 3 GB [MAX_ZIP_SIZE]
total size (compressed) : 7.85 GB
number of ZIP file (splited as MAX_ZIP_SIZE): 3
you have to change the value of MAX_ZIP_SIZE to 16(MB)10241024=16777216-22(zip header size)=16777194.
In my code, MAX_ZIP_SIZE set to 3 GB (ZIP has limitation of 4GB on various things).
final long MAX_ZIP_SIZE = 3221225472L; //3 GB
package com.company;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class QdeZip {
public static String createZIP(String directoryPath, String zipFileName, String filesToZip) {
try {
final int BUFFER = 104857600; // 100MB
final long MAX_ZIP_SIZE = 3221225472L; //3 GB
long currentSize = 0;
int zipSplitCount = 0;
String files[] = filesToZip.split(",");
if (!directoryPath.endsWith("/")) {
directoryPath = directoryPath + "/";
}
byte fileRAW[] = new byte[BUFFER];
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(directoryPath + zipFileName.toUpperCase()));
ZipEntry zipEntry;
FileInputStream entryFile;
for (String aFile : files) {
zipEntry = new ZipEntry(aFile);
if (currentSize >= MAX_ZIP_SIZE) {
zipSplitCount++;
//zipOut.closeEntry();
zipOut.close();
zipOut = new ZipOutputStream(new FileOutputStream(directoryPath + zipFileName.toLowerCase().replace(".zip", "_" + zipSplitCount + ".zip").toUpperCase()));
currentSize = 0;
}
zipOut.putNextEntry(zipEntry);
entryFile = new FileInputStream(directoryPath + aFile);
int count;
while ((count = entryFile.read(fileRAW, 0, BUFFER)) != -1) {
zipOut.write(fileRAW, 0, count);
//System.out.println("number of Bytes read = " + count);
}
entryFile.close();
zipOut.closeEntry();
currentSize += zipEntry.getCompressedSize();
}
zipOut.close();
//System.out.println(directory + " -" + zipFileName + " -Number of Files = " + files.length);
} catch (FileNotFoundException e) {
return "FileNotFoundException = " + e.getMessage();
} catch (IOException e) {
return "IOException = " + e.getMessage();
} catch (Exception e) {
return "Exception = " + e.getMessage();
}
return "1";
}
}
I have returned all Exception Messages as String to work with it. this
my own case related to project.
As far as I can see How to split a huge zip file into multiple volumes? just suggests keeping track of the archive size so far, and when it approaches some arbitrary value (which should be lower than your max), it'll decide to start a new file. So for a 16MB limit, you could set the value to 10MB and start a new zip whenever this is reached, but if you reach 9MB and your next file zips down to 8MB, you'll end up with a zip bigger than your limit.
The code given in that post didn't seem to work for me because 1) it got the size before the ZipEntry was created, so it was always 0 and 2) it didn't write out any zip :-) If I've got that wrong - let me know.
The following works for me. For simplicity, I've taken it out of the Wrapper and just have it all in main(String args[]). There are many, many ways this code could be improved :-)
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ChunkedZipTwo {
static final long MAX_LIMIT = 10 * 1000 * 1024; //10MB limit - hopefully this
public static void main(String[] args) throws Exception {
String[] files = {"file1", "file2", "file3"};
int i = 0;
boolean needNewFile = false;
long overallSize = 0;
ZipOutputStream out = getOutputStream(i);
byte[] buffer = new byte[1024];
for (String thisFileName : files) {
if (overallSize > MAX_LIMIT) {
out.close();
i++;
out = getOutputStream(i);
overallSize = 0;
}
FileInputStream in = new FileInputStream(thisFileName);
ZipEntry ze = new ZipEntry(thisFileName);
out.putNextEntry(ze);
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
in.close();
overallSize += ze.getCompressedSize();
}
out.close();
}
public static ZipOutputStream getOutputStream(int i) throws IOException {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream("bigfile" + i + ".zip"));
out.setLevel(Deflater.DEFAULT_COMPRESSION);
return out;
}
}

Categories