I have an angularJS based project with a PHP server. The goal at this point is to take a .jar file, and add json file to the META-INF folder in the .jar file and then download it back on the front end. Current solution seems to return an incomplete JAR file. It is openeable with 7Zip, but there is no content within the archive. I am unsure if the issue is the PHP and returning the file or the Angular. I am open to suggestions to do this differently.
My JS in the controller:
//Upload the Two Files:
exportFile = document.getElementById('jar-file-export');
exportFile.addEventListener('change',(event)=>{
//Get Jar File, and Save name for later use.
let i = document.getElementById('jar-file-export');
let formData = new FormData();
formData.append('jar', i.files[0]);
$scope.export.filename = i.files[0].name;
//GET JSON File:
let tFile = new Blob([JSON.stringify($scope.export.file)], {type: 'application/json'});
formData.append('descriptor',tFile);
$http({
method:"POST",
url:"assets/api/fileExport.php",
data: formData,
headers:{"Content-Type":undefined},
dataType: 'text',
mimeType: 'text/plain; charset=x-user-defined',
}).then(function success(response){
//saveAs = fileSaver.js
console.log('File Export,Success',response.data);
let data = response.data;
//Code copied from StackOverFlow:
let newContent = "";
for (var i = 0; i < data.length; i ) {
newContent = String.fromCharCode(data.charCodeAt(i) & 0xFF);
}
var bytes = new Uint8Array(newContent.length);
for (var i=0; i<newContent.length; i ) {
bytes[i] = newContent.charCodeAt(i);
}
blob = new Blob([bytes], {type: "application/zip"})
saveAs(blob, $scope.export.filename);
},function error(response){
console.log('File Export,error',response);
});
})
PHP:
<?php
switch($_SERVER["REQUEST_METHOD"]) {
case("POST"):{
$zip = new ZipArchive;
if ($zip->open($_FILES['jar']['tmp_name']) === true){
$zip->open($_FILES['jar']['tmp_name']);
$zip->addFile($_FILES['descriptor']['tmp_name'],file_get_contents($_FILES['descriptor']['tmp_name']));
$zip->close();
echo (file_get_contents($_FILES['jar']['tmp_name']));
}
break;
}
}
Additionally, I have tried just opening the JAR file and then returning it back to the client, and I still get an empty (but openable) jar file. The below results in a JAR that has a META-INF folder:
PHP:
<?php
switch($_SERVER["REQUEST_METHOD"]) {
case("POST"):{
$zip = new ZipArchive;
$newZip = new ZipArchive;
if ($zip->open($_FILES['jar']['tmp_name']) === true){
echo (file_get_contents($_FILES['jar']['tmp_name']));
}
break;
}
}
Any suggestions here would be greatly appreciated. Thanks
CodePudding user response:
On going through several iterations with lots of SO answers - I discovered that the PHP wasn't properly referencing the file location and modified it so that the PHP moved the files to temp folder.
$jar = $_FILES["jar"]["tmp_name"];
$desc = $_FILES["desc"]["tmp_name"];
$folder = "temp/";
$jar_name = $_FILES["jar"]["name"];
$desc_name = $_FILES["desc"]["name"];
move_uploaded_file($jar,$folder.$jar_name);
move_uploaded_file($desc,$folder.$desc_name);
//Open the Jar, add the descriptor and return the file name:
$zip = new ZipArchive();
if ($zip->open($folder.$jar_name) === TRUE){
$zip->addFile($folder.$desc_name,"META-INF/data.json");
$zip->close();
echo json_encode($jar_name);
} else {
echo "Failed to Open the JAR file";
}