This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Micro Frontends in Action and have Michael Geers on-line!
See this thread for details.
Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Creating a file from Various chunks of different files in java

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First,

I split 2 different pdf files into 20 KB chunks.

These 2 files are almost identical from beginning of the files, and a little different in end of file.
I mean, for example, one file(1.pdf) include " This is my testing ..... , that's all. thanks" . Then another file(2.pdf) include " This is my testing ..., that's all."
So , I get,
for 1.pdf
======
chunk1_1.pdf
chunk1_2.pdf
chunk1_3.pdf
chunk1_4.pdf
chunk1_5.pdf

and
for 2.pdf
======
chunk2_1.pdf
chunk2_2.pdf
chunk2_3.pdf
chunk2_4.pdf

The difference is only exist in the chunk1_5.pdf
So I tried to store only identical 4 chunks and the different one chunk1_5.pdf and try to reconstruct 1.pdf and 2.pdf by using those chunks

.

But even the new pdf file is shown in my folder, I can't open it.

Please help me,

 
Bartender
Posts: 9615
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch.
PDF's are not intended to be manipulated. If you want to create a PDF with some runtime data, your best bet is to create a document that is easy to manipulate (i.e. text, RTF, HTML) and use a conversion library (see here) to generate the PDF.
 
NyaLay Nyo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but, I try it re-combine those chunks from same file , it work well.
 
NyaLay Nyo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator






try {
while( ( count = in.read( b, 0, chunkSize ) ) > 0 ) {
bo.write( b, 0, count );

}
byte[] thebytes = bo.toByteArray();
return thebytes;
}
finally {
bo.close();
}
}
public static void main(String[] args)
{



try {

ByteUtils b=new ByteUtils();
ByteUtils a=new ByteUtils();


//This is merge, Begin
long b1l,b2l,b3l,b4l,b5l,b6l;
byte[] b1 = null,b2,b3,b4,b5,b6;
b1=b.loadBytesFromFile(args[0]);
b2=b.loadBytesFromFile(args[1]);
b3=b.loadBytesFromFile(args[2]);
b4=b.loadBytesFromFile(args[3]);
b5=b.loadBytesFromFile(args[4]);
b6=b.loadBytesFromFile(args[5]);
b1l=b1.length;
b2l=b2.length;
b3l=b3.length;
b4l=b4.length;
b5l=b5.length;
b6l=b6.length;

byte[] b7=new byte[(int)(b1l+b2l+b3l+b4l+b5l+b6l)];
for(x=0;x<b1.length;x++)
b7[x]=b1[x];

for(y=0;y<b2.length;y++)
{ b7[x]=b2[y];
x++;
}

for(y=0;y<b3.length;y++)
{ b7[x]=b3[y];
x++;
}

for(y=0;y<b4.length;y++)
{ b7[x]=b4[y];
x++;
}

for(y=0;y<b5.length;y++)
{ b7[x]=b5[y];
x++;
}

for(y=0;y<b6.length;y++)
{ if(b6[y]!=0){
b7[x]=b6[y];
x++;
}
else
{
continue;
}
}

a.saveBytesToFile(args[6], b7);

System.out.println("Finish Merging");


//This is merge,End





//This is split by fix length
// Start
/* byte[] b1=null;
long b1l;
b1=b.loadBytesFromFile(args[0]);
b1l=b1.length;
long toCtrl=b1l;
System.out.println("File Size :"+b1l);
double LENGTH;
int chunkSize=4096;
LENGTH=Math.ceil(b1.length/chunkSize)+1;

System.out.println(" number of Chunk :"+LENGTH);
int b_index=0;
int k=0;
byte[][] b3=new byte[(int) LENGTH][chunkSize];
while(toCtrl>0 && (k<LENGTH) )
{
if(toCtrl>=chunkSize){
for(int i=0;i<chunkSize;i++)
{ b3[k][i]=b1[b_index];
b_index++;
}
System.out.println("after 1 loop :"+b_index);
toCtrl-=chunkSize;
System.out.println("Left bytes to chunk :"+toCtrl);
}
else
{ byte[] bfinal=new byte[(int) toCtrl];
for(int i=0;i<toCtrl;i++)
{ bfinal[i]=b1[b_index];
b_index++;
}
b3[k]=bfinal;
System.out.println("after last loop :"+b_index);
toCtrl-=toCtrl;
}
k++;
System.out.println("k value :"+k);
}// end while(b_index<b1l)

//Write chunks to file
//for naming
String[] name = new String[(int) LENGTH];
String temp=null;
int fno;
for(int m=0;m<LENGTH;m++)
{ fno=m+1;
temp="c:\\tmp\\T2_".toString()+fno+".pdf".toString();
System.out.println(temp);
name[m]=temp;

}

//for actual write
for(int n=0;n<b3.length;n++)
{
asaveBytesToFile(name[n], b3[n]);
}*/
//End

// merge begin
/*long b1l,b2l,b3l,b4l,b5l,b6l,b7l;
byte[] b1 = null,b2,b3,b4,b5,b6,b8;
b1=b.loadBytesFromFile(args[0]);
b2=b.loadBytesFromFile(args[1]);
b3=b.loadBytesFromFile(args[2]);
b4=b.loadBytesFromFile(args[3]);
b5=b.loadBytesFromFile(args[4]);
b6=b.loadBytesFromFile(args[5]);
b8=b.loadBytesFromFile(args[6]);
b1l=b1.length;
b2l=b2.length;
b3l=b3.length;
b4l=b4.length;
b5l=b5.length;
b6l=b6.length;
b7l=b8.length;

byte[] b7=new byte[(int)(b1l+b2l+b3l+b4l+b5l+b6l+b7l)];
for(x=0;x<b1.length;x++)
b7[x]=b1[x];

for(y=0;y<b2.length;y++)
{ b7[x]=b2[y];
x++;
}

for(y=0;y<b3.length;y++)
{ b7[x]=b3[y];
x++;
}

for(y=0;y<b4.length;y++)
{ b7[x]=b4[y];
x++;
}

for(y=0;y<b5.length;y++)
{ b7[x]=b5[y];
x++;
}

for(y=0;y<b6.length;y++)
{ b7[x]=b6[y];
x++;
}

for(y=0;y<b8.length;y++)
{ if(b8[y]!=0){
b7[x]=b8[y];
x++;
}
else
{
continue;
}
}

a.saveBytesToFile(args[7], b7);

System.out.println("Finish Merging");
*/
//Merge end

System.out.println("Finish");





} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}



}









[/code]
 
Joe Ess
Bartender
Posts: 9615
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

NyaLay Nyo wrote:but, I try it re-combine those chunks from same file , it work well.



Sure, because you are putting the file back together the way it should be. Editing a PDF is non-trivial (see here) or there'd be more options available.
 
reply
    Bookmark Topic Watch Topic
  • New Topic