i have a server which sending thousands of tiny files like below:
static File file = null;
static File temp = null;
private static ServerSocket serverSocket;
private static Socket socket;
public static void main(String[] args) throws FileNotFoundException, IOException
{
serverSocket = new ServerSocket(3000);
socket = serverSocket.accept();
System.out.println("Connected");
File folder = new File("C:...\\Desktop\\thousands_of_tiny_files");
File[] listOfFiles = folder.listFiles();
File result[]=new File[listOfFiles.length];
byte [] bytearray = null;
FileInputStream fin =null;
BufferedInputStream bin = null;
for(int j=0;j<listOfFiles.length;j++){
String path= listOfFiles[j].getPath();
result=sendFile(path);
for(int i=0;i<result.length;i++)
{
fin = new FileInputStream(result[i]);
bin = new BufferedInputStream(fin);
bytearray = new byte [(int)result.length];
bin.read(bytearray,0,bytearray.length);
OutputStream os = socket.getOutputStream();
System.out.println("Sending Files "+ result[i]);
os.write(bytearray,0,bytearray.length);
os.flush();
System.out.println("File transfer completed");
}
}
fin.close();
bin.close();
socket.close();
}
public static File[] sendFile(String path)
{
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
File[] resultt = new File[listOfFiles.length];
for(int i=0;i<listOfFiles.length;i++)
{
temp=listOfFiles[i];
if(temp.isFile() && temp!=null)
resultt[i]=temp;
}
return resultt;
}
it is successfull but my problem is in client side. i dont know how to distinguish between the files and write them seperately into the client.
EDIT2: I changed the server side code like following using ZipOutputStream but still dont know how to unzip and write it in the client (mostly, dont know how to define the FileOutputStream in client:
for(int i=0;i<result.length;i++)
{
fin = new FileInputStream(result[i]);
bytearray = new byte [(int)result.length];
ZipOutputStream zipOpStream = new ZipOutputStream(socket.getOutputStream());
zipOpStream.putNextEntry(new ZipEntry(result[i].getName()));
System.out.println("Sending Files "+ result[i]);
zipOpStream.write(bytearray,0,bytearray.length);
zipOpStream.flush();
System.out.println("File transfer completed");
}
}
socket.close();
}
and the reciever code:
socket = new Socket("127.0.0.1",3000);
String outDir = "C:...\\Desktop\\here";
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
ZipInputStream zips = new ZipInputStream(bis);
ZipEntry zipEntry = null;
while(null != (zipEntry = zips.getNextEntry())){
String fileName = zipEntry.getName();
File outFile = new File(outDir + "/" + fileName);
System.out.println("----["+outFile.getName()+"], filesize["+zipEntry.getCompressedSize()+"]");
if(zipEntry.isDirectory()){
File zipEntryFolder = new File(zipEntry.getName());
if(zipEntryFolder.exists() == false){
outFile.mkdirs();
}
continue;
}else{
File parentFolder = outFile.getParentFile();
if(parentFolder.exists() == false){
parentFolder.mkdirs();
}
}
System.out.println("ZipEntry::"+zipEntry.getCompressedSize());
FileOutputStream fos = new FileOutputStream(outFile);
int fileLength = (int)zipEntry.getSize();
byte[] fileByte = new byte[fileLength];
zips.read(fileByte);
fos.write(fileByte);
fos.close();
socket.close();
}
now i get an exceptiom"negative NegativeArraySizeException"
Exception in thread "main" java.lang.NegativeArraySizeException
at recieve.Reciever.<init>(Reciever.java:71)
at recieve.Recieve$1.<init>(Recieve.java:28)
at recieve.Recieve.main(Recieve.java:28)
ZipEntry::-1
which probably is because of
int fileLength = (int)zipEntry.getSize();
but how can I solve this? i need the siz of next entry of zip folder for writing into the file. but the size is in Long not int